When using pl.render_labels with the color and palette parameter, the legends are incorrectly colored.
Example Plot:

Here we see that the Dot plot from scanpy counts 1.1K cells in the cluster NBL_9 and colors them with pastel green (correct color). However on the spatialdata plots, cluster NBL_11 is pastel green, when it should be NBL_9. The colors on the spatialdata image itself are not wrong, it is the colors on the legend which are incorrect.
This issue is occurs in pl.utils._get_palette, where the values in thecategories gets mapped to the colors of the palette. However in scanpy it's the categories of the DataFrame which get mapped to the colors.
By adjusting the following line from
|
return dict(zip(categories, palette)) |
|
|
to dict(zip(categories.categories, palette)) we get the correct legend-color mapping.

Reproducible Example - MIBITOF dataset
import spatialdata as sd
import spatialdata_plot
import matplotlib.pyplot as plt
from pathlib import Path
mibitof_sd = sd.read_zarr(Path("~/Downloads/data.zarr").expanduser())
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(18, 8), layout="tight")
mibitof_sd.pp.get_elements("point8_labels").pl.render_labels(color="Cluster").pl.show(
ax=axes[0]
)
mibitof_sd.pp.get_elements("point8_labels").pl.render_labels(
color="Cluster", palette=["blue"], groups=["Epithelial"]
).pl.show(ax=axes[1])
fig
Generated Plot: Here Imm_other in the legend actually represents the Epithelial cells.

When using
pl.render_labelswith thecolorandpaletteparameter, the legends are incorrectly colored.Example Plot:

Here we see that the Dot plot from
scanpycounts 1.1K cells in the clusterNBL_9and colors them with pastel green (correct color). However on thespatialdataplots, clusterNBL_11is pastel green, when it should beNBL_9. The colors on thespatialdataimage itself are not wrong, it is the colors on the legend which are incorrect.This issue is occurs in
pl.utils._get_palette, where the values in thecategoriesgets mapped to the colors of the palette. However inscanpyit's the categories of the DataFrame which get mapped to the colors.By adjusting the following line from
spatialdata-plot/src/spatialdata_plot/pl/utils.py
Lines 753 to 754 in 595ea44
to
dict(zip(categories.categories, palette))we get the correct legend-color mapping.Reproducible Example - MIBITOF dataset
Generated Plot: Here

Imm_otherin the legend actually represents theEpithelialcells.