@@ -1191,17 +1191,13 @@ class ListedColormap(Colormap):
1191
1191
the list will be extended by repetition.
1192
1192
"""
1193
1193
def __init__ (self , colors , name = 'from_list' , N = None ):
1194
- self .monochrome = False # Are all colors identical? (for contour.py)
1195
1194
if N is None :
1196
1195
self .colors = colors
1197
1196
N = len (colors )
1198
1197
else :
1199
1198
if isinstance (colors , str ):
1200
1199
self .colors = [colors ] * N
1201
- self .monochrome = True
1202
1200
elif np .iterable (colors ):
1203
- if len (colors ) == 1 :
1204
- self .monochrome = True
1205
1201
self .colors = list (
1206
1202
itertools .islice (itertools .cycle (colors ), N ))
1207
1203
else :
@@ -1211,7 +1207,6 @@ def __init__(self, colors, name='from_list', N=None):
1211
1207
pass
1212
1208
else :
1213
1209
self .colors = [gray ] * N
1214
- self .monochrome = True
1215
1210
super ().__init__ (name , N )
1216
1211
1217
1212
def _init (self ):
@@ -1220,6 +1215,21 @@ def _init(self):
1220
1215
self ._isinit = True
1221
1216
self ._set_extremes ()
1222
1217
1218
+ @property
1219
+ def monochrome (self ):
1220
+ """Return whether all colors in the colormap are identical."""
1221
+ # Replacement for the attribute *monochrome*. This ensures a consistent
1222
+ # response independent of the way the ListedColormap was created, which
1223
+ # was not the case for the manually set attribute.
1224
+ #
1225
+ # TODO: It's a separate discussion whether we need this property on
1226
+ # colormaps at all (at least as public API). It's a very special edge
1227
+ # case and we only use it for contours internally.
1228
+ if not self ._isinit :
1229
+ self ._init ()
1230
+
1231
+ return self .N <= 1 or np .all (self ._lut [0 ] == self ._lut [1 :self .N ])
1232
+
1223
1233
def resampled (self , lutsize ):
1224
1234
"""Return a new colormap with *lutsize* entries."""
1225
1235
colors = self (np .linspace (0 , 1 , lutsize ))
0 commit comments