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
4 changes: 1 addition & 3 deletions src/spatialdata_plot/pl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2479,9 +2479,7 @@ def _type_check_params(param_dict: dict[str, Any], element_type: str) -> dict[st
palette_group = param_dict.get("palette")
if element_type in ["shapes", "points", "labels"] and palette_group is not None and not isinstance(palette, dict):
groups = param_dict.get("groups")
if groups is None:
raise ValueError("When specifying 'palette', 'groups' must also be specified.")
if len(groups) != len(palette_group):
if groups is not None and len(groups) != len(palette_group):
raise ValueError(
f"The length of 'palette' and 'groups' must be the same, length is {len(palette_group)} and"
f"{len(groups)} respectively."
Expand Down
17 changes: 17 additions & 0 deletions tests/pl/test_render_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,23 @@ def test_plot_coloring_with_palette(self, sdata_blobs: SpatialData):
"blobs_polygons", color="cluster", groups=["c2", "c1"], palette=["green", "yellow"]
).pl.show()

def test_render_shapes_list_palette_without_groups(self, sdata_blobs: SpatialData):
# Regression test for #605: a list palette should map to categories in their natural order
# without requiring groups= to enumerate every category.
sdata_blobs["table"].obs["region"] = pd.Categorical(["blobs_polygons"] * sdata_blobs["table"].n_obs)
sdata_blobs["table"].uns["spatialdata_attrs"]["region"] = "blobs_polygons"
sdata_blobs.shapes["blobs_polygons"]["cluster"] = "c1"
sdata_blobs.shapes["blobs_polygons"].iloc[3:5, 1] = "c2"
sdata_blobs.shapes["blobs_polygons"]["cluster"] = sdata_blobs.shapes["blobs_polygons"]["cluster"].astype(
"category"
)

_, ax = plt.subplots()
sdata_blobs.pl.render_shapes("blobs_polygons", color="cluster", palette=["green", "yellow"]).pl.show(ax=ax)
legend = ax.get_legend()
assert legend is not None
assert {t.get_text() for t in legend.get_texts()} == {"c1", "c2"}

def test_plot_colorbar_respects_input_limits(self, sdata_blobs: SpatialData):
sdata_blobs["table"].obs["region"] = pd.Categorical(["blobs_polygons"] * sdata_blobs["table"].n_obs)
sdata_blobs["table"].uns["spatialdata_attrs"]["region"] = "blobs_polygons"
Expand Down
Loading