File tree 2 files changed +13
-3
lines changed
2 files changed +13
-3
lines changed Original file line number Diff line number Diff line change @@ -701,6 +701,8 @@ def _ensure_cmap(cmap):
701
701
"""
702
702
if isinstance (cmap , colors .Colormap ):
703
703
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
+ # use check_in_list to ensure type stability of the exception raised by
706
+ # the internal usage of this (ValueError vs KeyError)
707
+ _api .check_in_list (sorted (_colormaps ), cmap = cmap_name )
708
+ return mpl .colormaps [cmap_name ]
Original file line number Diff line number Diff line change @@ -1543,3 +1543,11 @@ def test_color_sequences():
1543
1543
plt .color_sequences .unregister ('rgb' ) # multiple unregisters are ok
1544
1544
with pytest .raises (ValueError , match = "Cannot unregister builtin" ):
1545
1545
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 )
You can’t perform that action at this time.
0 commit comments