Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitce488f5

Browse files
timhoffmgreglucas
andcommitted
Make ListedColormap.monochrome a property
The calculated property replaces the attribute *monochome*, which wasmanually set on `__init__`, but was not correctly set for all possibleinputs.This property ensures consistency and simplifies initialization at thecost of some computation overhead to determine whether the colormap ismonochrome.The computation cost is bearable (even without caching), because it'sonly used in `ContourSet._process_colors`.It's a separate discussion whether we need this property on colormaps atall (at least as public API). Usually, colormaps are not monochromeand monochrome colormaps are a very special edge case used in contoursonly. We may eventually deprecate it, but since it is currently publicAPI, let's leave it for now.There's also a technical API incompatibility in that users cannot setthe attribute anymore, but I'd argue that that has never been intendedand there's no practical use-case, so I refrain from the extra hassleof allowing setting this property.Co-authored-by: Greg Lucas <greg.m.lucas@gmail.com>
1 parentca39d41 commitce488f5

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
ifNisNone:
11961195
self.colors=colors
11971196
N=len(colors)
11981197
else:
11991198
ifisinstance(colors,str):
12001199
self.colors= [colors]*N
1201-
self.monochrome=True
12021200
elifnp.iterable(colors):
1203-
iflen(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+
defmonochrome(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+
ifnotself._isinit:
1229+
self._init()
1230+
1231+
returnself.N<=1ornp.all(self._lut[0]==self._lut[1:self.N])
1232+
12231233
defresampled(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
defreversed(self,name:str|None= ...)->LinearSegmentedColormap: ...
131131

132132
classListedColormap(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+
defmonochrome(self)->bool: ...
138139
defresampled(self,lutsize:int)->ListedColormap: ...
139140
defreversed(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+
deftest_monochrome():
78+
assertmcolors.ListedColormap(["red"]).monochrome
79+
assertmcolors.ListedColormap(["red"]*5).monochrome
80+
assertnotmcolors.ListedColormap(["red","green"]).monochrome
81+
82+
7783
deftest_colormaps_get_cmap():
7884
cr=mpl.colormaps
7985

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp