From f8c41a9d744a14386bbfe749137b6517fb2b0301 Mon Sep 17 00:00:00 2001 From: anon Date: Fri, 8 May 2026 16:34:51 +0200 Subject: [PATCH 1/2] Reject legend_loc='on data' loudly in pl.show() Closes #624. 'on data' requires scatter embedding coordinates, which do not exist for any spatialdata-plot render type. Previously crashed deep in scanpy with 'Grouper and axis must be same length' (categorical) or warned and fell back (channel legend). Now raises a clear ValueError at the API surface and the obsolete channel-legend fallback is removed. --- src/spatialdata_plot/pl/basic.py | 8 ++++++++ src/spatialdata_plot/pl/render.py | 6 ------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/spatialdata_plot/pl/basic.py b/src/spatialdata_plot/pl/basic.py index 8a7c2ec5..e237ac9e 100644 --- a/src/spatialdata_plot/pl/basic.py +++ b/src/spatialdata_plot/pl/basic.py @@ -1147,6 +1147,14 @@ def show( legend_fontoutline = legend_params.get("fontoutline", legend_fontoutline) na_in_legend = legend_params.get("na_in_legend", na_in_legend) + if legend_loc == "on data": + raise ValueError( + "legend_loc='on data' is not supported in spatialdata-plot: it requires " + "scatter embedding coordinates that do not exist for shapes, points, " + "labels, or image channels. Use 'right margin' (default), None, or any " + "matplotlib legend location string (e.g. 'upper right')." + ) + legend_params_obj = LegendParams( legend_fontsize=legend_fontsize, legend_fontweight=legend_fontweight, diff --git a/src/spatialdata_plot/pl/render.py b/src/spatialdata_plot/pl/render.py index 6a5bfc8c..7e1b8073 100644 --- a/src/spatialdata_plot/pl/render.py +++ b/src/spatialdata_plot/pl/render.py @@ -1153,12 +1153,6 @@ def _draw_channel_legend( palette_dict[entry.channel_name] = entry.color_hex legend_loc = legend_params.legend_loc - if legend_loc == "on data": - logger.warning( - "legend_loc='on data' is not supported for channel legends (no scatter coordinates); " - "falling back to 'right margin'." - ) - legend_loc = "right margin" categories = pd.Categorical(list(palette_dict)) From e9d997292e07f079acc913a1d67a0b71ea00ab1e Mon Sep 17 00:00:00 2001 From: anon Date: Fri, 8 May 2026 16:37:47 +0200 Subject: [PATCH 2/2] Shorten legend_loc='on data' error message --- src/spatialdata_plot/pl/basic.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/spatialdata_plot/pl/basic.py b/src/spatialdata_plot/pl/basic.py index e237ac9e..02d6dc51 100644 --- a/src/spatialdata_plot/pl/basic.py +++ b/src/spatialdata_plot/pl/basic.py @@ -1148,12 +1148,7 @@ def show( na_in_legend = legend_params.get("na_in_legend", na_in_legend) if legend_loc == "on data": - raise ValueError( - "legend_loc='on data' is not supported in spatialdata-plot: it requires " - "scatter embedding coordinates that do not exist for shapes, points, " - "labels, or image channels. Use 'right margin' (default), None, or any " - "matplotlib legend location string (e.g. 'upper right')." - ) + raise ValueError("legend_loc='on data' is not supported in spatialdata-plot.") legend_params_obj = LegendParams( legend_fontsize=legend_fontsize,