File tree 2 files changed +17
-11
lines changed 2 files changed +17
-11
lines changed Original file line number Diff line number Diff line change @@ -202,10 +202,6 @@ def get_cmap(self, cmap):
202
202
Returns
203
203
-------
204
204
Colormap
205
-
206
- Raises
207
- ------
208
- KeyError
209
205
"""
210
206
# get the default color map
211
207
if cmap is None :
@@ -214,9 +210,14 @@ def get_cmap(self, cmap):
214
210
# if the user passed in a Colormap, simply return it
215
211
if isinstance (cmap , colors .Colormap ):
216
212
return cmap
217
-
218
- # otherwise, it must be a string so look it up
219
- return self [cmap ]
213
+ if isinstance (cmap , str ):
214
+ _api .check_in_list (sorted (_colormaps ), cmap = cmap )
215
+ # otherwise, it must be a string so look it up
216
+ return self [cmap ]
217
+ raise TypeError (
218
+ 'get_cmap expects None or an instance of a str or Colormap . ' +
219
+ f'you passed { cmap !r} of type { type (cmap )} '
220
+ )
220
221
221
222
222
223
# public access to the colormaps should be via `matplotlib.colormaps`. For now,
Original file line number Diff line number Diff line change @@ -111,18 +111,23 @@ def test_register_cmap():
111
111
112
112
def test_colormaps_get_cmap ():
113
113
cr = mpl .colormaps
114
- new_cm = mcolors .ListedColormap (cr ["viridis" ].colors , name = 'v2' )
115
114
116
- # check None, str, and Colormap pass
115
+ # check str, and Colormap pass
117
116
assert cr .get_cmap ('plasma' ) == cr ["plasma" ]
118
117
assert cr .get_cmap (cr ["magma" ]) == cr ["magma" ]
119
118
120
- # check default default
119
+ # check default
121
120
assert cr .get_cmap (None ) == cr [mpl .rcParams ['image.cmap' ]]
121
+
122
+ # check ValueError on bad name
122
123
bad_cmap = 'AardvarksAreAwkward'
123
- with pytest .raises (KeyError , match = bad_cmap ):
124
+ with pytest .raises (ValueError , match = bad_cmap ):
124
125
cr .get_cmap (bad_cmap )
125
126
127
+ # check TypeError on bad type
128
+ with pytest .raises (TypeError , match = 'object' ):
129
+ cr .get_cmap (object ())
130
+
126
131
127
132
def test_double_register_builtin_cmap ():
128
133
name = "viridis"
You can’t perform that action at this time.
0 commit comments