@@ -1191,17 +1191,13 @@ class ListedColormap(Colormap):
11911191 the list will be extended by repetition.
11921192 """
11931193 def __init__ (self , colors , name = 'from_list' , N = None ):
1194- self .monochrome = False # Are all colors identical? (for contour.py)
11951194 if N is None :
11961195 self .colors = colors
11971196 N = len (colors )
11981197 else :
11991198 if isinstance (colors , str ):
12001199 self .colors = [colors ] * N
1201- self .monochrome = True
12021200 elif np .iterable (colors ):
1203- if len (colors ) == 1 :
1204- self .monochrome = True
12051201 self .colors = list (
12061202 itertools .islice (itertools .cycle (colors ), N ))
12071203 else :
@@ -1211,7 +1207,6 @@ def __init__(self, colors, name='from_list', N=None):
12111207 pass
12121208 else :
12131209 self .colors = [gray ] * N
1214- self .monochrome = True
12151210 super ().__init__ (name , N )
12161211
12171212 def _init (self ):
@@ -1220,6 +1215,21 @@ def _init(self):
12201215 self ._isinit = True
12211216 self ._set_extremes ()
12221217
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+
12231233 def resampled (self , lutsize ):
12241234 """Return a new colormap with *lutsize* entries."""
12251235 colors = self (np .linspace (0 , 1 , lutsize ))
0 commit comments