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

Skip to content

Commit 77da937

Browse files
committed
name as keyword variable and additional tests to multivariate colormaps
1 parent d365483 commit 77da937

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
raise KeyError(f"only 0 or 1 are"
Original file line numberDiff line numberDiff line change
@@ -1260,12 +1260,10 @@ class MultivarColormap:
12601260
Class for holding multiple `~matplotlib.colors.Colormap` for use in a
12611261
`~matplotlib.cm.VectorMappable` object
12621262
"""
1263-
def __init__(self, name, colormaps, combination_mode):
1263+
def __init__(self, colormaps, combination_mode, name='multivariate colormap'):
12641264
"""
12651265
Parameters
12661266
----------
1267-
name : str
1268-
The name of the colormap family.
12691267
colormaps: list or tuple of `~matplotlib.colors.Colormap` objects
12701268
The individual colormaps that are combined
12711269
combination_mode: str, 'sRGB_add' or 'sRGB_sub'
@@ -1275,6 +1273,8 @@ def __init__(self, name, colormaps, combination_mode):
12751273
`sRGB = cmap[0][X[0]] + cmap[1][x[1]] + ... + cmap[n-1][x[n-1]]`
12761274
- If 'sRGB_sub' -> Mixing produces darker colors
12771275
`sRGB = cmap[0][X[0]] + cmap[1][x[1]] + ... + cmap[n-1][x[n-1]] - n + 1`
1276+
name : str, optional
1277+
The name of the colormap family.
12781278
"""
12791279
self.name = name
12801280

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

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

14871484
@property
@@ -1520,32 +1517,33 @@ class BivarColormap:
15201517
lookup table. To be used with `~matplotlib.cm.VectorMappable`.
15211518
"""
15221519

1523-
def __init__(self, name, N=256, M=256, shape='square', origin=(0, 0)):
1520+
def __init__(self, N=256, M=256, shape='square', origin=(0, 0),
1521+
name='bivariate colormap'):
15241522
"""
15251523
Parameters
15261524
----------
1527-
name : str
1528-
The name of the colormap.
15291525
N : int
15301526
The number of RGB quantization levels along the first axis.
15311527
M : int
15321528
The number of RGB quantization levels along the second axis.
15331529
If None, M = N
15341530
shape: str 'square' or 'circle' or 'ignore' or 'circleignore'
15351531
1536-
- If 'square' each variate is clipped to [0,1] independently
1537-
- If 'circle' the variates are clipped radially to the center
1532+
- 'square' each variate is clipped to [0,1] independently
1533+
- 'circle' the variates are clipped radially to the center
15381534
of the colormap, and a circular mask is applied when the colormap
15391535
is displayed
1540-
- If 'ignore' the variates are not clipped, but instead assigned the
1536+
- 'ignore' the variates are not clipped, but instead assigned the
15411537
'outside' color
1542-
- If 'circleignore' a circular mask is applied, but the data is not
1538+
- 'circleignore' a circular mask is applied, but the data is not
15431539
clipped and instead assigned the 'outside' color
15441540
15451541
origin: (float, float)
15461542
The relative origin of the colormap. Typically (0, 0), for colormaps
15471543
that are linear on both axis, and (.5, .5) for circular colormaps.
15481544
Used when getting 1D colormaps from 2D colormaps.
1545+
name : str, optional
1546+
The name of the colormap.
15491547
"""
15501548

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

18701868
return new_cm
18711869

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

19511949
elif item == 1:
1952-
o = int(self._origin[0]*self.N)
1953-
if o > self.N-1:
1954-
o = self.N-1
1955-
one_d_lut = self._lut[o, :]
1950+
origin_0_as_int = int(self._origin[0]*self.N)
1951+
if origin_0_as_int > self.N-1:
1952+
origin_0_as_int = self.N-1
1953+
one_d_lut = self._lut[origin_0_as_int, :]
19561954
new_cmap = ListedColormap(one_d_lut, name=self.name+'_1', N=self.M)
19571955
else:
19581956
@@ -2033,8 +2031,6 @@ class SegmentedBivarColormap(BivarColormap):
20332031
----------
20342032
patch : nparray of shape (k, k, 3)
20352033
This patch gets supersamples to a lut of shape (N, M, 4)
2036-
name : str
2037-
The name of the colormap.
20382034
N : int
20392035
The number of RGB quantization levels along each axis.
20402036
shape: str 'square' or 'circle' or 'ignore' or 'circleignore'
@@ -2052,11 +2048,14 @@ class SegmentedBivarColormap(BivarColormap):
20522048
that are linear on both axis, and (.5, .5) for circular colormaps.
20532049
Used when getting 1D colormaps from 2D colormaps.
20542050
2051+
name : str, optional
2052+
The name of the colormap.
20552053
"""
20562054

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

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

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

21242123
def _init(self):
21252124
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