Skip to content

Legend order is now deterministic #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ and this project adheres to [Semantic Versioning][].
[keep a changelog]: https://keepachangelog.com/en/1.0.0/
[semantic versioning]: https://semver.org/spec/v2.0.0.html

## [0.0.5] -tbd

### Added

### Fixed

- Legend order is now deterministic (#143)

## [0.0.4] - 2023-08-11

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion src/spatialdata_plot/pl/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ def _get_collection_shape(
)
cax = ax.add_collection(_cax)

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

_ = _decorate_axs(
ax=ax,
Expand Down
4 changes: 2 additions & 2 deletions src/spatialdata_plot/pl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,11 +838,11 @@ def _get_palette(

if isinstance(palette, str):
cmap = plt.get_cmap(palette)
palette = [to_hex(x) for x in cmap(np.linspace(0, 1, len_cat), alpha=alpha)]
elif isinstance(palette, ListedColormap):
palette = [to_hex(x) for x in palette(np.linspace(0, 1, len_cat), alpha=alpha)]
cmap = palette
else:
raise TypeError(f"Palette is {type(palette)} but should be string or `ListedColormap`.")
palette = [to_hex(np.round(x, 5)) for x in cmap(np.linspace(0, 1, len_cat), alpha=alpha)]

return dict(zip(categories, palette))

Expand Down