-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[ENH]: Default matplotlib.colormaps[None]
to call matplotlib.colormaps[matplotlib.rcParams['image.cmap']]
?
#23981
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
Comments
If folks agree it's a regression error (& I think it kinda is) the fix is I think add something like if item is None:
item = mpl.rcParams['image.cmap'] which is a direct port of the matplotlib/lib/matplotlib/cm.py Lines 270 to 271 in a152851
before the try in: matplotlib/lib/matplotlib/cm.py Lines 91 to 95 in a152851
|
@mroeschke What is your use case? Are you passing in We have discussed this on the last dev call and are not yet clear how to handle the default colormap case in the new API. Logically, |
The use case would be for library code. In pandas, there are some APIs with matplotlib functionality that accept We're happy to start using |
@mroeschke I was going to suggest that you do something like
which should work on all versions of Matplotlib and leaves you an escape hatch if you want to make the default colormap controllable indepently. However, that probably is not enough for you as if you take When we decided to move to the registry model we mostly had end-users in mind, however this issues highlights that we did not take into account the library case enough. We have matplotlib/lib/matplotlib/cm.py Lines 686 to 708 in 0517187
if isinstance(inp, mcolors.Colormap):
cmap = inp
else:
cmap = mpl.colormaps[inp if inp is not None else mpl.rcParams["image.cmap"]] There is just enough complexity in there it probably be a function we provide. I think this has to be a function/method rather than shoehorned into If we have a registry method for this I propose def ensure_colormap(self, cmap: str|Colormap, * default=None):
if isinstance(cmap, mcolors.Colormap):
return cmap
default = default if default is not None else mpl.rcParams["image.cmap"]
cmap = cmap if cmap is not None else default
return self[cmap] which could also live as a free method and use the global registry. |
Yeah in pandas we used a similar workaround for translating I don't have any strong opinions personally on |
Discussed on the call, we are going to put the method on the registery class for 3.6.1 on the reasoning that this is not a new feature, but completing something that should have been in 3.6.1 as part of the deplication. |
After putting pending deprecations on `cm.get_cmap` we discovered that downstream libraries (pandas) were using the deprecated method to normalize between `None` (to get the default colormap), strings, and Colormap instances. This adds a method to `ColormapRegistry` to do this normalization. This can not replace our internal helper due to variations in what exceptions are raised. Closes matplotlib#23981
After putting pending deprecations on `cm.get_cmap` we discovered that downstream libraries (pandas) were using the deprecated method to normalize between `None` (to get the default colormap), strings, and Colormap instances. This adds a method to `ColormapRegistry` to do this normalization. This can not replace our internal helper due to variations in what exceptions are raised. Closes matplotlib#23981
After putting pending deprecations on `cm.get_cmap` we discovered that downstream libraries (pandas) were using the deprecated method to normalize between `None` (to get the default colormap), strings, and Colormap instances. This adds a method to `ColormapRegistry` to do this normalization. This can not replace our internal helper due to variations in what exceptions are raised. Closes matplotlib#23981
After putting pending deprecations on `cm.get_cmap` we discovered that downstream libraries (pandas) were using the deprecated method to normalize between `None` (to get the default colormap), strings, and Colormap instances. This adds a method to `ColormapRegistry` to do this normalization. This can not replace our internal helper due to variations in what exceptions are raised. Closes matplotlib#23981
Problem
While addressing the
matplotlib.cm.get_cmap
deprecation in 3.6:I noticed that
None
isn't directly migrate-ableProposed solution
It appears from the source that
get_cmap(None)
defaults tomatplotlib.rcParams['image.cmap']
so it would be nice ifcolormaps[None]
could default to that as well.Otherwise, it would be nice if this was better documented.
The text was updated successfully, but these errors were encountered: