# Really hard to plot categories palette

**URL:** <https://discourse.scverse.org/t/really-hard-to-plot-categories-palette/3486>\
**Category:** spatialdata\
**Created:** [February 14, 2025, 10:38pm UTC](https://discourse.scverse.org/t/really-hard-to-plot-categories-palette/3486 "2025-02-14T22:38:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![christophechu](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/christophechu/32/1062_2.png) [@christophechu](https://discourse.scverse.org/u/christophechu)\
**Post date:** [February 14, 2025, 10:38pm UTC](https://discourse.scverse.org/t/really-hard-to-plot-categories-palette/3486/1 "2025-02-14T22:38:03Z")

</div>

As I’ve mentioned in the github.

> <https://github.com/scverse/spatialdata-plot/issues/422>
>
> I already have my color palette stored in sdata.tables\["table"\].uns, but .pl.ren…der\_shapes() ignores it. As a result, I have to manually specify the palette in the function. However, this leads to errors related to invalid hex colors or missing groups.

> <https://github.com/scverse/spatialdata-plot/issues/414>
>
> When trying to plot a categorical column (say \`key\`) of \`sdata.tables\[table\_name…\].obs\` using spatialdata-plot, colors specified in \`sdata.tables\[table\_name\].uns\["{key}\_colors" \]\` are ignored due to this line:
> 
> https://github.com/scverse/spatialdata-plot/blob/478ab27f9e8a6e06fc28c411e7721f1332b34b66/src/spatialdata\_plot/pl/utils.py#L758
> 
> This PR fixes this;
> https://github.com/scverse/spatialdata-plot/pull/413
> 
> Minimal example to reproduce the issue:
> 
> \`\`\`
> import numpy as np
> import pandas as pd
> from anndata import AnnData
> from spatialdata import get\_element\_instances
> from spatialdata.datasets import blobs
> from spatialdata.models import TableModel
> 
> import spatialdata\_plot
> 
> RNG = np.random.default\_rng(seed=42)
> 
> labels\_name = "blobs\_labels"
> 
> sdata\_blobs = blobs()
> 
> instances = get\_element\_instances(sdata\_blobs\[labels\_name\])
> n\_obs = len(instances)
> adata = AnnData(
> RNG.normal(size=(n\_obs, 10)),
> obs=pd.DataFrame(RNG.normal(size=(n\_obs, 3)), columns=\["a", "b", "c"\]),
> )
> adata.obs\["instance\_id"\] = instances.values
> adata.obs\["category"\] = RNG.choice(\["a", "b", "c"\], size=adata.n\_obs)
> adata.obs\["category"\]\[:3\] = \["a", "b", "c"\]
> adata.obs\["region"\] = labels\_name
> table = TableModel.parse(
> adata=adata,
> region\_key="region",
> instance\_key="instance\_id",
> region=labels\_name,
> )
> sdata\_blobs\["other\_table"\] = table
> sdata\_blobs\["other\_table"\].obs\["category"\] = sdata\_blobs\["other\_table"\].obs\["category"\].astype("category")
> 
> sdata\_blobs\["other\_table"\].uns\["category\_colors"\] = \["#800080", "#008000", "#FFFF00"\] #purple, green ,yellow
> \# placeholder, otherwise "category\_colors" will be ignored
> sdata\_blobs\["other\_table"\].uns\["category"\] = "\_\_value\_\_"
> sdata\_blobs.pl.render\_labels("blobs\_labels", color="category").pl.show()
> \`\`\`
> 
> With the small fix this gives
> 
> !\[Image\](https://github.com/user-attachments/assets/ce931be1-fb8e-4765-9323-451bd9202266)
> 
> 
> I've added a unit test which reproduces the issue in the PR https://github.com/ArneDefauw/spatialdata-plot/blob/5af65aa118f7abf87e47470038ecdbddb27ef1ca/tests/pl/test\_render\_labels.py#L217
> 
> As explained in the PR, I've stumbled upon an issue when trying to plot a subset of the data, trying to maintain the same colors, see https://github.com/ArneDefauw/spatialdata-plot/blob/5af65aa118f7abf87e47470038ecdbddb27ef1ca/tests/pl/test\_render\_labels.py#L225.
> 
> Starting from the code above, if we do:
> 
> \`\`\`
> sdata\_blobs = bounding\_box\_query(
> sdata\_blobs,
> axes=("y", "x"),
> min\_coordinate=\[0, 0\],
> max\_coordinate=\[100, 100\],
> target\_coordinate\_system="global",
> )
> sdata\_blobs.pl.render\_labels("blobs\_labels", color="category").pl.show()
> \`\`\`
> 
> we get:
> 
> !\[Image\](https://github.com/user-attachments/assets/0638d9bc-bda2-4cab-8aaf-f09ad87f9b56)
> 
> while we expect 
> 
> !\[Image\](https://github.com/user-attachments/assets/5a70bce8-b515-4112-88c7-bebdf3eeea44)
> 
> This issue is caused by https://github.com/scverse/spatialdata/blob/03d3be80fad69ff54097e90a9e80ad02e9e0e242/src/spatialdata/\_utils.py#L203.
> 
> There is a workaround for this:
> 
> \`\`\`
> sdata\_blobs\["other\_table"\].obs\["category"\] = (
> sdata\_blobs\["other\_table"\].obs\["category"\].cat.remove\_unused\_categories()
> )
> \`\`\`
> 
> as documented here https://github.com/ArneDefauw/spatialdata-plot/blob/5af65aa118f7abf87e47470038ecdbddb27ef1ca/tests/pl/test\_render\_labels.py#L250, but this should probably be documented somewhere public.

> <https://github.com/scverse/spatialdata-plot/issues/391>
>
> Hi,
> An normal hex color is a string include seven chars, such as "#DD4958", but …pl.render\_shapes uses nine chars when using datashader, which is not compatible with palette in stored in sdata\["table"\].uns\["leiden\_colors"\]:
> !\[Image\](https://github.com/user-attachments/assets/f67f713c-c67b-44ce-bc54-125e3d7cfa9c)
> 
> This is my palette:
> \['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b'\]
> So ian error occurred when using customized palette:
> !\[Image\](https://github.com/user-attachments/assets/d8314793-b52f-46e1-a1fa-2a3ab0226050)

How to plot with our own palette?

---

<div class="post-metadata">

**Author:** ![LucaMarconato](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/lucamarconato/32/661_2.png) [@LucaMarconato](https://discourse.scverse.org/u/LucaMarconato)\
**Post date:** [February 14, 2025, 10:58pm UTC](https://discourse.scverse.org/t/really-hard-to-plot-categories-palette/3486/2 "2025-02-14T22:58:21Z")

</div>

Hi, thanks for gathering information across the issues. Working on this is near the top of my todo list (but currently I am working on a few other issues, so it will take a bit).

Meanwhile please complement the issue you opened [Really hard to plot categories palette.... · Issue #422 · scverse/spatialdata-plot · GitHub](https://github.com/scverse/spatialdata-plot/issues/422) with a working example using the `blobs()` dataset, like it is done here: [Plot categorical color when colors are provided via .uns · Issue #414 · scverse/spatialdata-plot · GitHub](https://github.com/scverse/spatialdata-plot/issues/414). This will make investigating the issue easier and we generally prioritize issues that are complemented with a minimal reproducible example. Thanks!
