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

Skip to content

Commit 0b6b385

Browse files
committed
FIX: ensure type stability for missing cmaps in set_cmap
Previously this would raise a `ValueError`, the refactor caused it to raise `KeyError`. This inadvertently located a bad test in xarray, but restoring the old behavior.
1 parent 07af522 commit 0b6b385

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/matplotlib/cm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,6 @@ def _ensure_cmap(cmap):
701701
"""
702702
if isinstance(cmap, colors.Colormap):
703703
return cmap
704-
return mpl.colormaps[
705-
cmap if cmap is not None else mpl.rcParams['image.cmap']
706-
]
704+
cmap_name = cmap if cmap is not None else mpl.rcParams["image.cmap"]
705+
_api.check_in_list(sorted(_colormaps), cmap=cmap_name)
706+
return mpl.colormaps[cmap_name]

lib/matplotlib/tests/test_colors.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,3 +1543,11 @@ def test_color_sequences():
15431543
plt.color_sequences.unregister('rgb') # multiple unregisters are ok
15441544
with pytest.raises(ValueError, match="Cannot unregister builtin"):
15451545
plt.color_sequences.unregister('tab10')
1546+
1547+
1548+
def test_cm_set_cmap_error():
1549+
sm = cm.ScalarMappable()
1550+
# Pick a name we are pretty sure will never be a colormap name
1551+
bad_cmap = 'AardvarksAreAwkward'
1552+
with pytest.raises(ValueError, match=bad_cmap):
1553+
sm.set_cmap(bad_cmap)

0 commit comments

Comments
 (0)