Skip to content

Commit 88f6928

Browse files
Colorbar no longer autoscales to [0, 1] (#155)
Co-authored-by: Tim Treis <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4dd12f2 commit 88f6928

File tree

8 files changed

+22
-7
lines changed

8 files changed

+22
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ and this project adheres to [Semantic Versioning][].
1212

1313
### Added
1414

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

1918
### Fixed
2019

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

2425
## [0.0.4] - 2023-08-11
2526

src/spatialdata_plot/pl/basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def render_shapes(
152152
layer: str | None = None,
153153
palette: ListedColormap | str | None = None,
154154
cmap: Colormap | str | None = None,
155-
norm: None | Normalize = None,
155+
norm: bool | Normalize = False,
156156
na_color: str | tuple[float, ...] | None = "lightgrey",
157157
outline_alpha: float = 1.0,
158158
fill_alpha: float = 1.0,

src/spatialdata_plot/pl/render.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ def _render_shapes(
9090
alpha=render_params.fill_alpha,
9191
)
9292

93+
values_are_categorical = color_source_vector is not None
94+
9395
# color_source_vector is None when the values aren't categorical
94-
if color_source_vector is None and render_params.transfunc is not None:
96+
if values_are_categorical and render_params.transfunc is not None:
9597
color_vector = render_params.transfunc(color_vector)
9698

9799
norm = copy(render_params.cmap_params.norm)
@@ -113,15 +115,17 @@ def _render_shapes(
113115
# **kwargs,
114116
)
115117

118+
# Sets the limits of the colorbar to the values instead of [0, 1]
119+
if not norm and not values_are_categorical:
120+
_cax.set_clim(min(color_vector), max(color_vector))
121+
116122
cax = ax.add_collection(_cax)
117123

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

123-
# print(len(set(color_vector)) == 1)
124-
# print(set(color_source_vector[0]) == to_hex(render_params.cmap_params.na_color))
125129
if not (
126130
len(set(color_vector)) == 1 and list(set(color_vector))[0] == to_hex(render_params.cmap_params.na_color)
127131
):
@@ -190,7 +194,7 @@ def _render_points(
190194
key=render_params.color,
191195
palette=render_params.palette,
192196
)
193-
# print(p)
197+
194198
color_source_vector, color_vector, _ = _set_color_source_vec(
195199
sdata=sdata_filt,
196200
element=points,

src/spatialdata_plot/pl/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ def _get_scalebar(
564564

565565
def _prepare_cmap_norm(
566566
cmap: Colormap | str | None = None,
567-
norm: Normalize | Sequence[Normalize] | None = None,
567+
norm: Normalize | bool = False,
568568
na_color: str | tuple[float, ...] = (0.0, 0.0, 0.0, 0.0),
569569
vmin: float | None = None,
570570
vmax: float | None = None,
@@ -576,6 +576,8 @@ def _prepare_cmap_norm(
576576

577577
if isinstance(norm, Normalize):
578578
pass # TODO
579+
elif not norm:
580+
pass
579581
elif vcenter is None:
580582
norm = Normalize(vmin=vmin, vmax=vmax)
581583
else:
Loading
Loading
Loading

tests/pl/test_render_shapes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,11 @@ def test_plot_can_color_from_geodataframe(self, sdata_blobs: SpatialData):
100100

101101
def test_plot_can_scale_shapes(self, sdata_blobs: SpatialData):
102102
sdata_blobs.pl.render_shapes(elements="blobs_circles", scale=0.5).pl.show()
103+
104+
def test_plot_colorbar_respects_input_limits(self, sdata_blobs: SpatialData):
105+
sdata_blobs.shapes["blobs_polygons"]["cluster"] = [1, 2, 3, 5, 20]
106+
sdata_blobs.pl.render_shapes("blobs_polygons", color="cluster", groups=["c1"]).pl.show()
107+
108+
def test_plot_colorbar_can_be_normalised(self, sdata_blobs: SpatialData):
109+
sdata_blobs.shapes["blobs_polygons"]["cluster"] = [1, 2, 3, 5, 20]
110+
sdata_blobs.pl.render_shapes("blobs_polygons", color="cluster", groups=["c1"], norm=True).pl.show()

0 commit comments

Comments
 (0)