Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ and this project adheres to [Semantic Versioning][].

### Added

- Multipolygons are now handled correctly (#93)
- Can now scale shapes (#152)
- Can now plot columns from GeoDataFrame (#149)

### Fixed

- Multipolygons are now handled correctly (#93)
- Legend order is now deterministic (#143)
- Images no longer normalised by default (#150)
- Colorbar no longer autoscales to [0, 1] (#155)

## [0.0.4] - 2023-08-11

Expand Down
2 changes: 1 addition & 1 deletion src/spatialdata_plot/pl/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def render_shapes(
layer: str | None = None,
palette: ListedColormap | str | None = None,
cmap: Colormap | str | None = None,
norm: None | Normalize = None,
norm: bool | Normalize = False,
na_color: str | tuple[float, ...] | None = "lightgrey",
outline_alpha: float = 1.0,
fill_alpha: float = 1.0,
Expand Down
12 changes: 8 additions & 4 deletions src/spatialdata_plot/pl/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ def _render_shapes(
alpha=render_params.fill_alpha,
)

values_are_categorical = color_source_vector is not None

# color_source_vector is None when the values aren't categorical
if color_source_vector is None and render_params.transfunc is not None:
if values_are_categorical and render_params.transfunc is not None:
color_vector = render_params.transfunc(color_vector)

norm = copy(render_params.cmap_params.norm)
Expand All @@ -113,15 +115,17 @@ def _render_shapes(
# **kwargs,
)

# Sets the limits of the colorbar to the values instead of [0, 1]
if not norm and not values_are_categorical:
_cax.set_clim(min(color_vector), max(color_vector))

cax = ax.add_collection(_cax)

# Using dict.fromkeys here since set returns in arbitrary order
palette = (
ListedColormap(dict.fromkeys(color_vector)) if render_params.palette is None else render_params.palette
)

# print(len(set(color_vector)) == 1)
# print(set(color_source_vector[0]) == to_hex(render_params.cmap_params.na_color))
if not (
len(set(color_vector)) == 1 and list(set(color_vector))[0] == to_hex(render_params.cmap_params.na_color)
):
Expand Down Expand Up @@ -190,7 +194,7 @@ def _render_points(
key=render_params.color,
palette=render_params.palette,
)
# print(p)

color_source_vector, color_vector, _ = _set_color_source_vec(
sdata=sdata_filt,
element=points,
Expand Down
4 changes: 3 additions & 1 deletion src/spatialdata_plot/pl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ def _get_scalebar(

def _prepare_cmap_norm(
cmap: Colormap | str | None = None,
norm: Normalize | Sequence[Normalize] | None = None,
norm: Normalize | bool = False,
na_color: str | tuple[float, ...] = (0.0, 0.0, 0.0, 0.0),
vmin: float | None = None,
vmax: float | None = None,
Expand All @@ -576,6 +576,8 @@ def _prepare_cmap_norm(

if isinstance(norm, Normalize):
pass # TODO
elif not norm:
pass
elif vcenter is None:
norm = Normalize(vmin=vmin, vmax=vmax)
else:
Expand Down
Binary file modified tests/_images/Shapes_can_color_from_geodataframe.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions tests/pl/test_render_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,11 @@ def test_plot_can_color_from_geodataframe(self, sdata_blobs: SpatialData):

def test_plot_can_scale_shapes(self, sdata_blobs: SpatialData):
sdata_blobs.pl.render_shapes(elements="blobs_circles", scale=0.5).pl.show()

def test_plot_colorbar_respects_input_limits(self, sdata_blobs: SpatialData):
sdata_blobs.shapes["blobs_polygons"]["cluster"] = [1, 2, 3, 5, 20]
sdata_blobs.pl.render_shapes("blobs_polygons", color="cluster", groups=["c1"]).pl.show()

def test_plot_colorbar_can_be_normalised(self, sdata_blobs: SpatialData):
sdata_blobs.shapes["blobs_polygons"]["cluster"] = [1, 2, 3, 5, 20]
sdata_blobs.pl.render_shapes("blobs_polygons", color="cluster", groups=["c1"], norm=True).pl.show()