Skip to content

Commit a77e2f6

Browse files
authored
Legend order is now deterministic (#143)
1 parent 81070f3 commit a77e2f6

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ and this project adheres to [Semantic Versioning][].
88
[keep a changelog]: https://keepachangelog.com/en/1.0.0/
99
[semantic versioning]: https://semver.org/spec/v2.0.0.html
1010

11+
## [0.0.5] -tbd
12+
13+
### Added
14+
15+
### Fixed
16+
17+
- Legend order is now deterministic (#143)
18+
1119
## [0.0.4] - 2023-08-11
1220

1321
### Fixed

src/spatialdata_plot/pl/render.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ def _get_collection_shape(
180180
)
181181
cax = ax.add_collection(_cax)
182182

183-
palette = ListedColormap(set(color_vector)) if render_params.palette is None else render_params.palette
183+
# Using dict.fromkeys here since set returns in arbitrary order
184+
palette = ListedColormap(dict.fromkeys(color_vector)) if render_params.palette is None else render_params.palette
184185

185186
_ = _decorate_axs(
186187
ax=ax,

src/spatialdata_plot/pl/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,11 +838,11 @@ def _get_palette(
838838

839839
if isinstance(palette, str):
840840
cmap = plt.get_cmap(palette)
841-
palette = [to_hex(x) for x in cmap(np.linspace(0, 1, len_cat), alpha=alpha)]
842841
elif isinstance(palette, ListedColormap):
843-
palette = [to_hex(x) for x in palette(np.linspace(0, 1, len_cat), alpha=alpha)]
842+
cmap = palette
844843
else:
845844
raise TypeError(f"Palette is {type(palette)} but should be string or `ListedColormap`.")
845+
palette = [to_hex(np.round(x, 5)) for x in cmap(np.linspace(0, 1, len_cat), alpha=alpha)]
846846

847847
return dict(zip(categories, palette))
848848

0 commit comments

Comments
 (0)