Skip to content

Commit 8717705

Browse files
timtreisclaude
andcommitted
Warn when groups values don't match color categories (#455)
When `groups` contains values absent from the `color` column's categories, emit a warning so users understand why the plot is empty. Also add a defensive empty-input guard in `_get_collection_shape` to prevent a `KeyError: 'geometry'` if an empty DataFrame ever reaches PatchCollection construction. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 23010b9 commit 8717705

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/spatialdata_plot/pl/render.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,14 @@ def _render_shapes(
302302
# Only show non-matching elements if the user explicitly sets na_color.
303303
_na = render_params.cmap_params.na_color
304304
if groups is not None and values_are_categorical and (_na.default_color_set or _na.alpha == "00"):
305+
assert color_source_vector is not None # guaranteed by values_are_categorical
306+
_groups_list = [groups] if isinstance(groups, str) else groups
307+
_missing = set(_groups_list) - set(color_source_vector.categories)
308+
if _missing:
309+
logger.warning(
310+
f"Groups {sorted(_missing)} not found in the values of '{col_for_color}'. "
311+
"The `groups` parameter filters values of the `color` column."
312+
)
305313
keep, color_source_vector, color_vector = _filter_groups_transparent_na(
306314
groups, color_source_vector, color_vector
307315
)
@@ -754,6 +762,13 @@ def _render_points(
754762
# Only show non-matching elements if the user explicitly sets na_color.
755763
_na = render_params.cmap_params.na_color
756764
if groups is not None and color_source_vector is not None and (_na.default_color_set or _na.alpha == "00"):
765+
_groups_list = [groups] if isinstance(groups, str) else groups
766+
_missing = set(_groups_list) - set(color_source_vector.categories)
767+
if _missing:
768+
logger.warning(
769+
f"Groups {sorted(_missing)} not found in the values of '{col_for_color}'. "
770+
"The `groups` parameter filters values of the `color` column."
771+
)
757772
keep, color_source_vector, color_vector = _filter_groups_transparent_na(
758773
groups, color_source_vector, color_vector
759774
)

src/spatialdata_plot/pl/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,9 @@ def _create_patches(
553553
shapes_df, fill_c.tolist(), outline_c.tolist() if hasattr(outline_c, "tolist") else outline_c, s
554554
)
555555

556+
if patches.empty:
557+
return PatchCollection([], **kwargs)
558+
556559
return PatchCollection(
557560
patches["geometry"].values.tolist(),
558561
snap=False,

0 commit comments

Comments
 (0)