Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 40a321f

Browse files
committed
FIX: Use colormap objects in rcParams when setting cmap
image.cmap previously used a string, which would lookup that string in the colormap registry. This caused issues if there was a mismatched registration name versus attribute name. Instead if we get a colormap object, we should just use that directly rather than using any name to look things up.
1 parent 16a8477 commit 40a321f

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

lib/matplotlib/cm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,9 +715,9 @@ def _ensure_cmap(cmap):
715715
Colormap
716716
717717
"""
718-
if isinstance(cmap, colors.Colormap):
719-
return cmap
720718
cmap_name = cmap if cmap is not None else mpl.rcParams["image.cmap"]
719+
if isinstance(cmap_name, colors.Colormap):
720+
return cmap_name
721721
# use check_in_list to ensure type stability of the exception raised by
722722
# the internal usage of this (ValueError vs KeyError)
723723
_api.check_in_list(sorted(_colormaps), cmap=cmap_name)

lib/matplotlib/pyplot.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,13 +2180,10 @@ def set_cmap(cmap):
21802180
matplotlib.cm.register_cmap
21812181
matplotlib.cm.get_cmap
21822182
"""
2183-
# The name of the colormap may be different than the registered name.
2184-
# We want the registered name for the rc setting
2185-
name = getattr(cmap, "name", cmap)
2186-
# Run the cmap (str or Colormap) through get_cmap to validate
2187-
# the cmap before updating the rc parameters
2183+
# Run the cmap (str or Colormap) through get_cmap to validate the cmap
2184+
# and get an instance of Colormap before updating the rc parameters
21882185
cmap = get_cmap(cmap)
2189-
rc('image', cmap=name)
2186+
rc('image', cmap=cmap)
21902187
im = gci()
21912188

21922189
if im is not None:

0 commit comments

Comments
 (0)