Skip to content

Commit bb2de5e

Browse files
committed
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")`.
1 parent 7a5458b commit bb2de5e

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

doc/users/next_whats_new/2019-06-28-AL.rst

+4
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ first copy the colormap and set the extreme colors on that copy.
1111
The new `.Colormap.set_extremes` method is provided for API symmetry with
1212
`.Colormap.with_extremes`, but note that it suffers from the same issue as the
1313
earlier individual setters.
14+
15+
Additionally, it is now possible to set :rc:`image.cmap` to a `.Colormap`
16+
instance, such as a new colormap created with `~.Colormap.set_extremes`. (This
17+
can only be done from Python code, not from the :file:`matplotlibrc` file.)

lib/matplotlib/rcsetup.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from matplotlib import _api, animation, cbook
2626
from matplotlib.cbook import ls_mapper
2727
from matplotlib.fontconfig_pattern import parse_fontconfig_pattern
28-
from matplotlib.colors import is_color_like
28+
from matplotlib.colors import Colormap, is_color_like
2929

3030
# Don't let the original cycler collide with our validating cycler
3131
from cycler import Cycler, cycler as ccycler
@@ -393,6 +393,13 @@ def validate_color(s):
393393

394394
validate_colorlist = _listify_validator(
395395
validate_color, allow_stringlist=True, doc='return a list of colorspecs')
396+
397+
398+
def _validate_cmap(s):
399+
cbook._check_isinstance((str, Colormap), cmap=s)
400+
return s
401+
402+
396403
validate_orientation = ValidateInStrings(
397404
'orientation', ['landscape', 'portrait'], _deprecated_since="3.3")
398405

@@ -1141,7 +1148,7 @@ def _convert_validator_spec(key, conv):
11411148

11421149
"image.aspect": validate_aspect, # equal, auto, a number
11431150
"image.interpolation": validate_string,
1144-
"image.cmap": validate_string, # gray, jet, etc.
1151+
"image.cmap": _validate_cmap, # gray, jet, etc.
11451152
"image.lut": validate_int, # lookup table
11461153
"image.origin": ["upper", "lower"],
11471154
"image.resample": validate_bool,

0 commit comments

Comments
 (0)