From bb2de5e02d89a70cec5854154ae1f2b03419912a Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 13 Dec 2020 13:49:23 +0100 Subject: [PATCH] Support setting rcParams["image.cmap"] to Colormap instances. This makes it possible to set the rcParam to a non-registered colormap (which cannot be referred to by name). A use-case is to set it to e.g. `viridis.with_extremes(bad="k")`. --- doc/users/next_whats_new/2019-06-28-AL.rst | 4 ++++ lib/matplotlib/rcsetup.py | 11 +++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/doc/users/next_whats_new/2019-06-28-AL.rst b/doc/users/next_whats_new/2019-06-28-AL.rst index 8dc22b1cc2d7..f127b2c1c158 100644 --- a/doc/users/next_whats_new/2019-06-28-AL.rst +++ b/doc/users/next_whats_new/2019-06-28-AL.rst @@ -11,3 +11,7 @@ first copy the colormap and set the extreme colors on that copy. The new `.Colormap.set_extremes` method is provided for API symmetry with `.Colormap.with_extremes`, but note that it suffers from the same issue as the earlier individual setters. + +Additionally, it is now possible to set :rc:`image.cmap` to a `.Colormap` +instance, such as a new colormap created with `~.Colormap.set_extremes`. (This +can only be done from Python code, not from the :file:`matplotlibrc` file.) diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index e6c943fdac59..d00cbed8f25f 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -25,7 +25,7 @@ from matplotlib import _api, animation, cbook from matplotlib.cbook import ls_mapper from matplotlib.fontconfig_pattern import parse_fontconfig_pattern -from matplotlib.colors import is_color_like +from matplotlib.colors import Colormap, is_color_like # Don't let the original cycler collide with our validating cycler from cycler import Cycler, cycler as ccycler @@ -393,6 +393,13 @@ def validate_color(s): validate_colorlist = _listify_validator( validate_color, allow_stringlist=True, doc='return a list of colorspecs') + + +def _validate_cmap(s): + cbook._check_isinstance((str, Colormap), cmap=s) + return s + + validate_orientation = ValidateInStrings( 'orientation', ['landscape', 'portrait'], _deprecated_since="3.3") @@ -1141,7 +1148,7 @@ def _convert_validator_spec(key, conv): "image.aspect": validate_aspect, # equal, auto, a number "image.interpolation": validate_string, - "image.cmap": validate_string, # gray, jet, etc. + "image.cmap": _validate_cmap, # gray, jet, etc. "image.lut": validate_int, # lookup table "image.origin": ["upper", "lower"], "image.resample": validate_bool,