-
Notifications
You must be signed in to change notification settings - Fork 686
feat(plotting): Add group_colors parameter to sc.pl.dotplot to specify unique, perceptually uniform colormaps for each group #3764
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
base: main
Are you sure you want to change the base?
Conversation
b5e8003 to
6855232
Compare
|
Good idea! However I’m concerned that the color maps aren’t visually comparable.
|
|
Thanks for the feedback! The visual comparability is a good point, I didn’t think about that. Since colorspacious is unmaintained, perhaps we can check some alternatives. After a quick search, I found this post on the Oklab color space which might be another way to implement this: Converting from linear sRGB to Oklab. |
|
Hi @flying-sheep, I finally found some time to further look into this. I started with your GitHub Gist notebook but using the colour package. I used the OKLab color space to generate sequential colormaps by varying lightness while keeping a and b channels constant. I added a second example generating white-to-color gradients by interpolating all channels in OKLab. This is the implementation I would use to create colormaps on the fly for the DotPlot feature. Here's the gist with the examples: Is this what you had in mind? Then I'd go ahead and integrate this into DotPlot. |
|
I’m super sorry that I didn’t write back. Yes, this is exactly what I was thinking! |
f8e225e to
39cb5d0
Compare
a161c5b to
e897d9f
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3764 +/- ##
==========================================
+ Coverage 76.89% 77.02% +0.12%
==========================================
Files 117 117
Lines 12450 12539 +89
==========================================
+ Hits 9574 9658 +84
- Misses 2876 2881 +5
Flags with carried forward coverage won't be shown. Click here to find out more.
|
e897d9f to
26060d2
Compare
… coloring - Adds the group_cmaps parameter to sc.pl.dotplot, allowing users to specify a unique colormap for each group. - Implements a corresponding stacked colorbar legend (_plot_stacked_colorbars) to display the multiple colormaps, with group labels for clarity. - Adds robust error handling, raising a ValueError if a plotted group is not defined in the group_cmaps dictionary. - Fixes a pre-existing layout bug (dots overlapping in size legend, especially when dendrogram=True) by setting the legends width to 2.0 (compared to DEFAULT_LEGENDS_WIDTH = 1.5) inside the dotplot wrapper function. - Refactors data preparation logic from DotPlot.__init__ into a new _prepare_dot_data helper method. This was done to improve code quality and resolve pre-commit linter errors related to complexity. - Includes comprehensive tests for the new functionality, swap-axis compatibility, and error conditions.
…erpolation - Implemented group_colors parameter accepting dict of colors - Added OKLab white-to-color gradient generation in _utils.py - Updated DotPlot to use dynamic ListedColormaps - Added colour-science as optional dependency - Updated default legend width to 2.0 to fix layout bugs - Updated reference images for dotplots and affected plots
5155c5e to
4b15dd6
Compare
9c20b29 to
5776567
Compare
456adc8 to
b20011a
Compare
for more information, see https://pre-commit.ci
|
@flying-sheep No worries! I had not found the time before anyway. But now I integrated the colour-science package to generate perceptually uniform white-to-color gradients via the OKLab color space. This avoids the out-of-gamut issues we discussed and produces really clean gradients for the dot plots. I added it as an optional dependency in A few additional improvements I included:
I’ve updated the PR description at the top with the full technical details and an example snippet. Let me know if this needs any other tweaks! |
Description
This PR introduces a new feature to
sc.pl.dotplot: thegroup_colorsparameter. This allows users to assign a unique color to each category in a dot plot, which automatically generates perceptually uniform white-to-color gradients using the OKLab color space. This is particularly useful for creating publication-ready figures that align with a paper's established color scheme for different cell types.Key Changes
New Feature:
group_colorsparametersc.pl.dotplotand theDotPlotclass that accepts a dictionary mapping group names to colors (e.g.,{'T-cell': 'blue', 'B-cell': '#aa40fc'}).colour-sciencelibrary.OKLab Color Gradient Function
_create_white_to_color_gradient()function in_utils.pythat creates smooth, perceptually uniform colormaps from white to any target color.Optional Dependency:
colour-sciencecolour-scienceas an optional dependency under[project.optional-dependencies.plotting]inpyproject.toml.group_colorsis used withoutcolour-scienceinstalled, a clearImportErroris raised with installation instructions.colourmarker inmarks.pyfor tests that require this dependency.Stacked Colorbar Legend
_plot_stacked_colorbarsmethod) that displays individual colorbars for each group whengroup_colorsis used.Fallback Behavior & Warnings
group_colorsfall back to the default colormap (or a customcmapif provided).UserWarningis emitted listing which groups will use the default colormap.UserWarningis raised when bothcmapandgroup_colorsare specified, informing users thatcmapwill only be used as a fallback.Internal Refactoring
DotPlot.__init__method by extracting logic into a new helper function_prepare_dot_data.pre-commitstandards and improves the readability of the initialization process.Layout & Testing Improvements
Dotplot Legend Spacing
1.5to2.0within thedotplotwrapper.dendrogram=True).expected.png) for several existing dotplot tests have been updated to reflect this cleaner, non-overlapping layout.Test Suite Adjustments
test_dotplot_group_colors_coverage_mockto ensure full coverage without addingscanpy[plotting]topyproject.toml, which prevents regressions in low-vers CI environments and led to failure of the patch coverage test.test_rank_genes_groupsto prevent flaky failures caused by minor rendering shifts in the new environment.Files Modified
src/scanpy/plotting/_dotplot.py: Addedgroup_colorslogic, legend handling, and layout width update.src/scanpy/plotting/_utils.py: Added_create_white_to_color_gradient()helper.tests/test_plotting.py: Addedgroup_colorstest cases, a mock coverage test, and updated tolerance for rank genes.pyproject.toml: Addedcolour-scienceto optional dependencies.src/testing/scanpy/_pytest/marks.py- Addedcolourmarker for optional dependencyExample Usage