8000 Merge pull request #29125 from timhoffm/colormap-monochrome · matplotlib/matplotlib@464c8e8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 464c8e8

Browse files
authored
Merge pull request #29125 from timhoffm/colormap-monochrome
Make ListedColormap.monochrome a property
2 parents 1c4a554 + ce488f5 commit 464c8e8

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

lib/matplotlib/colors.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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))

lib/matplotlib/colors.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,12 @@ class LinearSegmentedColormap(Colormap):
130130
def reversed(self, name: str | None = ...) -> LinearSegmentedColormap: ...
131131

132132
class ListedColormap(Colormap):
133-
monochrome: bool
134133
colors: ArrayLike | ColorType
135134
def __init__(
136135
self, colors: ArrayLike | ColorType, name: str = ..., N: int | None = ...
137136
) -> None: ...
137+
@property
138+
def monochrome(self) -> bool: ...
138139
def resampled(self, lutsize: int) -> ListedColormap: ...
139140
def reversed(self, name: str | None = ...) -> ListedColormap: ...
140141

lib/matplotlib/tests/test_colors.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ def test_resampled():
7474
assert_array_almost_equal(lc(np.nan), lc3(np.nan))
7575

7676

77+
def test_monochrome():
78+
assert mcolors.ListedColormap(["red"]).monochrome
79+
assert mcolors.ListedColormap(["red"] * 5).monochrome
80+
assert not mcolors.ListedColormap(["red", "green"]).monochrome
81+
82+
7783
def test_colormaps_get_cmap():
7884
cr = mpl.colormaps
7985

0 commit comments

Comments
 (0)
0