8000 name as keyword variable and additional tests to multivariate colormaps · matplotlib/matplotlib@74b3fbf · GitHub
[go: up one dir, main page]

Skip to content

Commit 74b3fbf

Browse files
committed
name as keyword variable and additional tests to multivariate colormaps
1 parent b59850d commit 74b3fbf

File tree

5 files changed

+90
-63
lines changed

5 files changed

+90
-63
lines changed

lib/matplotlib/_cm_bivar.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,8 +1305,8 @@
13051305

13061306
cmaps = {
13071307
"BiPeak": SegmentedBivarColormap(
1308-
BiPeak, "BiPeak", 256, "square", (.5, .5)),
1308+
BiPeak, 256, "square", (.5, .5), name="BiPeak"),
13091309
"BiOrangeBlue": SegmentedBivarColormap(
1310-
BiOrangeBlue, "BiOrangeBlue", 256, "square", (0, 0)),
1311-
"BiCone": SegmentedBivarColormap(BiPeak, "BiCone", 256, "circle", (.5, .5)),
1310+
BiOrangeBlue, 256, "square", (0, 0), name="BiOrangeBlue"),
1311+
"BiCone": SegmentedBivarColormap(BiPeak, 256, "circle", (.5, .5), name="BiCone"),
13121312
}

lib/matplotlib/_cm_multivar.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,10 @@
157157
]}
158158

159159
cmap_families = {
160-
'2VarAddA': MultivarColormap('2VarAddA', [cmaps['2VarAddA' + str(i)] for
161-
i in range(2)], 'sRGB_add'),
162-
'2VarSubA': MultivarColormap('2VarSubA', [cmaps['2VarSubA' + str(i)] for
163-
i in range(2)], 'sRGB_sub'),
164-
'3VarAddA': MultivarColormap('3VarAddA', [cmaps['3VarAddA' + str(i)] for
165-
i in range(3)], 'sRGB_add'),
160+
'2VarAddA': MultivarColormap([cmaps['2VarAddA' + str(i)] for i in range(2)],
161+
'sRGB_add', name='2VarAddA'),
162+
'2VarSubA': MultivarColormap([cmaps['2VarSubA' + str(i)] for i in range(2)],
163+
'sRGB_sub', name='2VarSubA'),
164+
'3VarAddA': MultivarColormap([cmaps['3VarAddA' + str(i)] for i in range(3)],
165+
'sRGB_add', name='3VarAddA'),
166166
}

lib/matplotlib/colors.py

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,12 +1259,10 @@ class MultivarColormap:
12591259
Class for holding multiple `~matplotlib.colors.Colormap` for use in a
12601260
`~matplotlib.cm.VectorMappable` object
12611261
"""
1262-
def __init__(self, name, colormaps, combination_mode):
1262+
def __init__(self, colormaps, combination_mode, name='multivariate colormap'):
12631263
"""
12641264
Parameters
12651265
----------
1266-
name : str
1267-
The name of the colormap family.
12681266
colormaps: list or tuple of `~matplotlib.colors.Colormap` objects
12691267
The individual colormaps that are combined
12701268
combination_mode: str, 'sRGB_add' or 'sRGB_sub'
@@ -1274,6 +1272,8 @@ def __init__(self, name, colormaps, combination_mode):
12741272
`sRGB = cmap[0][X[0]] + cmap[1][x[1]] + ... + cmap[n-1][x[n-1]]`
12751273
- If 'sRGB_sub' -> Mixing produces darker colors
12761274
`sRGB = cmap[0][X[0]] + cmap[1][x[1]] + ... + cmap[n-1][x[n-1]] - n + 1`
1275+
name : str, optional
1276+
The name of the colormap family.
12771277
"""
12781278
self.name = name
12791279

@@ -1283,12 +1283,11 @@ def __init__(self, name, colormaps, combination_mode):
12831283
raise ValueError("A MultivarColormap must have more than one colormap.")
12841284
colormaps = list(colormaps) # ensure cmaps is a list, i.e. not a tuple
12851285
for i, cmap in enumerate(colormaps):
1286-
if not isinstance(cmap, Colormap):
1287-
if isinstance(cmap, str):
1288-
colormaps[i] = mpl.colormaps[cmap]
1289-
else:
1290-
raise ValueError("colormaps must be a list of objects that subclass"
1291-
" Colormap or valid strings.")
1286+
if isinstance(cmap, str):
1287+
colormaps[i] = mpl.colormaps[cmap]
1288+
elif not isinstance(cmap, Colormap):
1289+
raise ValueError("colormaps must be a list of objects that subclass"
1290+
" Colormap or valid strings.")
12921291

12931292
self._colormaps = colormaps
12941293
if combination_mode not in ['sRGB_add', 'sRGB_sub']:
@@ -1313,7 +1312,7 @@ def __call__(self, X, alpha=None, bytes=False, clip=True):
13131312
self[i] is colormap i.
13141313
alpha : float or array-like or None
13151314
Alpha must be a scalar between 0 and 1, a sequence of such
1316-
floats with shape matching Xi, or None.
1315+
floats with shape matching *Xi*, or None.
13171316
bytes : bool
13181317
If False (default), the returned RGBA values will be floats in the
13191318
interval ``[0, 1]`` otherwise they will be `numpy.uint8`\s in the
@@ -1472,15 +1471,13 @@ def with_extremes(self, *, bad=None, under=None, over=None):
14721471
raise ValueError("*under* must contain a color for each scalar colormap"
14731472
f" i.e. be of length {len(new_cm)}.")
14741473
else:
1475-
for c, b in zip(new_cm, under):
1476-
c.set_under(b)
1474+
[c.set_under(b) for c, b in zip(new_cm, under)]
14771475
if over is not None:
14781476
if not np.iterable(over) or not len(over) == len(new_cm):
14791477
raise ValueError("*over* must contain a color for each scalar colormap"
14801478
f" i.e. be of length {len(new_cm)}.")
14811479
else:
1482-
for c, b in zip(new_cm, over):
1483-
c.set_over(b)
1480+
[c.set_over(b) for c, b in zip(new_cm, over)]
14841481
return new_cm
14851482

14861483
@property
@@ -1519,32 +1516,33 @@ class BivarColormap:
15191516
lookup table. To be used with `~matplotlib.cm.VectorMappable`.
15201517
"""
15211518

1522-
def __init__(self, name, N=256, M=256, shape='square', origin=(0, 0)):
1519+
def __init__(self, N=256, M=256, shape='square', origin=(0, 0),
1520+
name='bivariate colormap'):
15231521
"""
15241522
Parameters
15251523
----------
1526-
name : str
1527-
The name of the colormap.
15281524
N : int
15291525
The number of RGB quantization levels along the first axis.
15301526
M : int
15311527
The number of RGB quantization levels along the second axis.
15321528
If None, M = N
15331529
shape: str 'square' or 'circle' or 'ignore' or 'circleignore'
15341530
1535-
- If 'square' each variate is clipped to [0,1] independently
1536-
- If 'circle' the variates are clipped radially to the center
1531+
- 'square' each variate is clipped to [0,1] independently
1532+
- 'circle' the variates are clipped radially to the center
15371533
of the colormap, and a circular mask is applied when the colormap
15381534
is displayed
1539-
- If 'ignore' the variates are not clipped, but instead assigned the
1535+
- 'ignore' the variates are not clipped, but instead assigned the
15401536
'outside' color
1541-
- If 'circleignore' a circular mask is applied, but the data is not
1537+
- 'circleignore' a circular mask is applied, but the data is not
15421538
clipped and instead assigned the 'outside' color
15431539
15441540
origin: (float, float)
15451541
The relative origin of the colormap. Typically (0, 0), for colormaps
15461542
that are linear on both axis, and (.5, .5) for circular colormaps.
15471543
Used when getting 1D colormaps from 2D colormaps.
1544+
name : str, optional
1545+
The name of the colormap.
15481546
"""
15491547

15501548
self.name = name
@@ -1864,7 +1862,7 @@ def with_extremes(self, *, bad=None, outside=None, shape=None, origin=None):
18641862
raise ValueError("The shape must be a valid string, "
18651863
"'square', 'circle', 'ignore', or 'circleignore'")
18661864
if origin is not None:
1867-
self._origin = (float(origin[0]), float(origin[1]))
1865+
new_cm._origin = (float(origin[0]), float(origin[1]))
18681866

18691867
return new_cm
18701868

@@ -1941,17 +1939,17 @@ def __getitem__(self, item):
19411939
if not self._isinit:
19421940
self._init()
19431941
if item == 0:
1944-
o = int(self._origin[1]*self.M)
1945-
if o > self.M-1:
1946-
o = self.M-1
1947-
one_d_lut = self._lut[:, o]
1942+
origin_1_as_int = int(self._origin[1]*self.M)
1943+
if origin_1_as_int > self.M-1:
1944+
origin_1_as_int = self.M-1
1945+
one_d_lut = self._lut[:, origin_1_as_int]
19481946
new_cmap = ListedColormap(one_d_lut, name=self.name+'_0', N=self.N)
19491947

19501948
elif item == 1:
1951-
o = int(self._origin[0]*self.N)
1952-
if o > self.N-1:
1953-
o = self.N-1
1954-
one_d_lut = self._lut[o, :]
1949+
origin_0_as_int = int(self._origin[0]*self.N)
1950+
if origin_0_as_int > self.N-1:
1951+
origin_0_as_int = self.N-1
1952+
one_d_lut = self._lut[origin_0_as_int, :]
19551953
new_cmap = ListedColormap(one_d_lut, name=self.name+'_1', N=self.M)
19561954
else:
19571955
raise KeyError(f"only 0 or 1 are"
@@ -2032,8 +2030,6 @@ class SegmentedBivarColormap(BivarColormap):
20322030
----------
20332031
patch : nparray of shape (k, k, 3)
20342032
This patch gets supersamples to a lut of shape (N, M, 4)
2035-
name : str
2036-
The name of the colormap.
20372033
N : int
20382034
The number of RGB quantization levels along each axis.
20392035
shape: str 'square' or 'circle' or 'ignore' or 'circleignore'
@@ -2051,11 +2047,14 @@ class SegmentedBivarColormap(BivarColormap):
20512047
that are linear on both axis, and (.5, .5) for circular colormaps.
20522048
Used when getting 1D colormaps from 2D colormaps.
20532049
2050+
name : str, optional
2051+
The name of the colormap.
20542052
"""
20552053

2056-
def __init__(self, patch, name, N=256, shape='square', origin=(0, 0)):
2054+
def __init__(self, patch, N=256, shape='square', origin=(0, 0),
2055+
name='segmented bivariate colormap'):
20572056
self.patch = patch
2058-
super().__init__(name, N, N, shape, origin)
2057+
super().__init__(N, N, shape, origin, name=name)
20592058

20602059
def _init(self):
20612060
s = self.patch.shape
@@ -2079,8 +2078,6 @@ class BivarColormapFromImage(BivarColormap):
20792078
----------
20802079
lut : nparray of shape (N, M, 3) or (N, M, 4)
20812080
The look-up-table
2082-
name : str
2083-
The name of the colormap.
20842081
shape: str 'square' or 'circle' or 'ignore' or 'circleignore'
20852082
20862083
- If 'square' each variate is clipped to [0,1] independently
@@ -2095,10 +2092,12 @@ class BivarColormapFromImage(BivarColormap):
20952092
The relative origin of the colormap. Typically (0, 0), for colormaps
20962093
that are linear on both axis, and (.5, .5) for circular colormaps.
20972094
Used when getting 1D colormaps from 2D colormaps.
2095+
name : str, optional
2096+
The name of the colormap.
20982097
20992098
"""
21002099

2101-
def __init__(self, lut, name='', shape='square', origin=(0, 0)):
2100+
def __init__(self, lut, shape='square', origin=(0, 0), name='from image'):
21022101
# We can allow for a PIL.Image as unput in the following way, but importing
21032102
# matplotlib.image.pil_to_array() results in a circular import
21042103
# For now, this function only accepts numpy arrays.
@@ -2118,7 +2117,7 @@ def __init__(self, lut, name='', shape='square', origin=(0, 0)):
21182117
new_lut[:, :, 3] = 1.
21192118
lut = new_lut
21202119
self._lut = lut
2121-
super().__init__(name, lut.shape[0], lut.shape[1], shape, origin)
2120+
super().__init__(lut.shape[0], lut.shape[1], shape, origin, name=name)
21222121

21232122
def _init(self):
21242123
self._isinit = True

lib/matplotlib/colors.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class MultivarColormap:
142142
name: str
143143
colormaps: list[Colormap]
144144
n_variates: int
145-
def __init__(self, name: str, colormaps: list[Colormap], combination_mode: str) -> None: ...
145+
def __init__(self, colormaps: list[Colormap], combination_mode: str, name: str = ...) -> None: ...
146146
@overload
147147
def __call__(
148148
self, X: Sequence[Sequence[float]] | np.ndarray, alpha: ArrayLike | None = ..., bytes: bool = ..., clip: bool = ...
@@ -179,7 +179,7 @@ class BivarColormap:
179179
N: int
180180
M: int
181181
n_variates: int
182-
def __init__(self, name: str, N: int = ..., M: int | None = ..., shape: str = ..., origin: Sequence[float] = ...
182+
def __init__(self, N: int = ..., M: int | None = ..., shape: str = ..., origin: Sequence[float] = ..., name: str = ...
183183
) -> None: ...
184184
@overload
185185
def __call__(
@@ -221,11 +221,11 @@ class BivarColormap:
221221

222222
class SegmentedBivarColormap(BivarColormap):
223223
def __init__(
224-
self, patch: np.ndarray, name: str, N: int = ..., shape: str = ..., origin: Sequence[float] = ...
224+
self, patch: np.ndarray, N: int = ..., shape: str = ..., origin: Sequence[float] = ..., name: str = ...
225225
) -> None: ...
226226

227227
class BivarColormapFromImage(BivarColormap):
228-
def __init__(self, lut: np.ndarray, name: str = ..., shape: str = ..., origin: Sequence[float] = ...
228+
def __init__(self, lut: np.ndarray, shape: str = ..., origin: Sequence[float] = ..., name: str = ...
229229
) -> None: ...
230230

231231
class Normalize:

0 commit comments

Comments
 (0)
0