Skip to content

ENH: Add bad, under, over kwargs to Colormap #29460

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
merged 1 commit into from
Jan 24, 2025

Conversation

timhoffm
Copy link
Member

This is part of the effort for making Colormaps immutable (#29141). Obviously, we can only get immutable in the future if the bad, under, over colors can already be set upon creation.

@timhoffm timhoffm force-pushed the colormap-bad-under-over branch from 182afa1 to a2d9e63 Compare January 11, 2025 22:45
@github-actions github-actions bot added the Documentation: examples files in galleries/examples label Jan 11, 2025
@timhoffm timhoffm force-pushed the colormap-bad-under-over branch from a2d9e63 to 0ab0843 Compare January 11, 2025 22:47
@timhoffm timhoffm mentioned this pull request Jan 11, 2025
8 tasks
@@ -1215,6 +1247,18 @@ class ListedColormap(Colormap):
N > len(colors)

the list will be extended by repetition.

.. deprecated:: 3.11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems orthogonal to this PR? Or is this already deprecated elsewhere and this is just a doc cleanup? I'm not really following why we would need to do this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The deprecation is in #29135, it's indeed unrelated. I just noticed that I haven't added this to the parameter docs, and didn't bother to make a separate PR since I was editing that position in the docstring anyway. Sorry for the confusion.

@timhoffm timhoffm force-pushed the colormap-bad-under-over branch from 0ab0843 to dfb2f68 Compare January 11, 2025 23:15
@timhoffm timhoffm added this to the v3.11.0 milestone Jan 11, 2025
Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure about immutability of colormaps, but this addition seems helpful even with the current state of things.

@timhoffm
Copy link
Member Author

The annoying thing with mutable colormaps is that we always have to do copies when returning colormaps from the , because a user could do cmap = plt.colormaps['viridis']; cmap.set_over('red') and we don't want users to modify the global colormaps. Likewise, but more subtile: For plt.imshow(a, cmap=my_cmap); my_cmap.set_over('red'); plt.imshow(b, cmap=my_cmap) should the over value be applied in the first image?

Changes to colormaps are relatively rare. The most prominent ones are over/under/bad, but again most of them are just setup-time changes (and done separately because the constructor did not supply them). Conceptually a colormap with a given name is fixed. You don't want it to behave differently at different. Therefore, immutability is logically helpful here, and immutable objects are much easier to maintain and reason about.

"""
Parameters
----------
name : str
The name of the colormap.
N : int
The number of RGB quantization levels.
bad : :mpltype:`color`, default: transparent
The color for invalid values (NaN or masked).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The color for invalid values (NaN or masked).
The color for invalid values (NaN or masked).
.. versionadded:: 3.11

? (Likewise in many locations below. Hence, maybe it is not required...)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. It's a bit verbose, but OTOH it's good to be explicit about this.

Copy link
Member

@oscargus oscargus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subject to deciding if the .. versionadded:: should be added. Feel free to self merge when you have decided.

This is part of the effort for making Colormaps immutable (matplotlib#29141).
Obviously, we can only get immutable in the future if the
bad, under, over colors can already be set upon creation.
@timhoffm timhoffm force-pushed the colormap-bad-under-over branch from dfb2f68 to e1adce7 Compare January 24, 2025 09:52
@timhoffm timhoffm merged commit 0a0b9ac into matplotlib:main Jan 24, 2025
41 checks passed
@timhoffm timhoffm deleted the colormap-bad-under-over branch January 24, 2025 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants