Skip to content

Commit 3442d35

Browse files
committed
Changes accompanying MultiNorm
These changes accompany MultiNorm but do not affect the MultiNorm class itself.
1 parent dbeca30 commit 3442d35

File tree

3 files changed

+46
-29
lines changed

3 files changed

+46
-29
lines changed

lib/matplotlib/colorizer.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,13 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True):
154154
# Otherwise run norm -> colormap pipeline
155155
x = ma.asarray(x)
156156
if norm:
157-
x = self.norm(x)
157+
if isinstance(self.norm, colors.MultiNorm):
158+
# using structured_output=False gives one less
159+
# memcopy operation
160+
x = self.norm(x, structured_output=False)
161+
else:
162+
x = self.norm(x)
163+
158164
rgba = self.cmap(x, alpha=alpha, bytes=bytes)
159165
return rgba
160166

lib/matplotlib/colors.py

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ def __init__(self, name, N=256, *, bad=None, under=None, over=None):
745745
self._i_over = self.N + 1
746746
self._i_bad = self.N + 2
747747
self._isinit = False
748-
self.n_variates = 1
748+
self.n_components = 1
749749
#: When this colormap exists on a scalar mappable and colorbar_extend
750750
#: is not False, colorbar creation will pick up ``colorbar_extend`` as
751751
#: the default value for the ``extend`` keyword in the
@@ -1414,10 +1414,10 @@ def __init__(self, colormaps, combination_mode, name='multivariate colormap'):
14141414
combination_mode: str, 'sRGB_add' or 'sRGB_sub'
14151415
Describe how colormaps are combined in sRGB space
14161416
1417-
- If 'sRGB_add' -> Mixing produces brighter colors
1418-
`sRGB = sum(colors)`
1419-
- If 'sRGB_sub' -> Mixing produces darker colors
1420-
`sRGB = 1 - sum(1 - colors)`
1417+
- If 'sRGB_add': Mixing produces brighter colors
1418+
``sRGB = sum(colors)``
1419+
- If 'sRGB_sub': Mixing produces darker colors
1420+
``sRGB = 1 - sum(1 - colors)``
14211421
name : str, optional
14221422
The name of the colormap family.
14231423
"""
@@ -1438,22 +1438,24 @@ def __init__(self, colormaps, combination_mode, name='multivariate colormap'):
14381438
self._colormaps = colormaps
14391439
_api.check_in_list(['sRGB_add', 'sRGB_sub'], combination_mode=combination_mode)
14401440
self._combination_mode = combination_mode
1441-
self.n_variates = len(colormaps)
1441+
self.n_components = len(colormaps)
14421442
self._rgba_bad = (0.0, 0.0, 0.0, 0.0) # If bad, don't paint anything.
14431443

14441444
def __call__(self, X, alpha=None, bytes=False, clip=True):
14451445
r"""
14461446
Parameters
14471447
----------
14481448
X : tuple (X0, X1, ...) of length equal to the number of colormaps
1449-
X0, X1 ...:
1450-
float or int, `~numpy.ndarray` or scalar
1449+
or structured array with multiple fields, X0, X1, ...:
1450+
Xi: float or int, `~numpy.ndarray` or scalar.
14511451
The data value(s) to convert to RGBA.
1452-
For floats, *Xi...* should be in the interval ``[0.0, 1.0]`` to
1453-
return the RGBA values ``X*100`` percent along the Colormap line.
1454-
For integers, *Xi...* should be in the interval ``[0, self[i].N)`` to
1455-
return RGBA values *indexed* from colormap [i] with index ``Xi``, where
1456-
self[i] is colormap i.
1452+
1453+
- For floats, *Xi...* should be in the interval ``[0.0, 1.0]`` to
1454+
return the RGBA values ``X*100`` percent along the line of colormap i.
1455+
- For integers, *Xi...* should be in the interval ``[0, self[i].N)`` to
1456+
return RGBA values *indexed* from colormap i with
1457+
index ``Xi``, where self[i] is colormap i.
1458+
14571459
alpha : float or array-like or None
14581460
Alpha must be a scalar between 0 and 1, a sequence of such
14591461
floats with shape matching *Xi*, or None.
@@ -1470,10 +1472,14 @@ def __call__(self, X, alpha=None, bytes=False, clip=True):
14701472
RGBA values with a shape of ``X.shape + (4, )``.
14711473
"""
14721474

1475+
if isinstance(X, np.ndarray) and X.dtype.fields is not None:
1476+
X = tuple(X[descriptor[0]] for descriptor in X.dtype.descr)
1477+
14731478
if len(X) != len(self):
14741479
raise ValueError(
14751480
f'For the selected colormap the data must have a first dimension '
1476-
f'{len(self)}, not {len(X)}')
1481+
f'{len(self)}, not {len(X)}, or be structured array ',
1482+
f'with {len(self)} fields.')
14771483
rgba, mask_bad = self[0]._get_rgba_and_mask(X[0], bytes=False)
14781484
for c, xx in zip(self[1:], X[1:]):
14791485
sub_rgba, sub_mask_bad = c._get_rgba_and_mask(xx, bytes=False)
@@ -1561,7 +1567,7 @@ def resampled(self, lutshape):
15611567
Parameters
15621568
----------
15631569
lutshape : tuple of (`int`, `None`)
1564-
The tuple must have a length matching the number of variates.
1570+
The tuple must have a length matching the number of components.
15651571
For each element in the tuple, if `int`, the corresponding colorbar
15661572
is resampled, if `None`, the corresponding colorbar is not resampled.
15671573
@@ -1589,15 +1595,15 @@ def with_extremes(self, *, bad=None, under=None, over=None):
15891595
15901596
Parameters
15911597
----------
1592-
bad: :mpltype:`color`, default: None
1598+
bad : :mpltype:`color`, default: None
15931599
If Matplotlib color, the bad value is set accordingly in the copy
15941600
1595-
under tuple of :mpltype:`color`, default: None
1596-
If tuple, the `under` value of each component is set with the values
1601+
under : tuple of :mpltype:`color`, default: None
1602+
If tuple, the ``under`` value of each component is set with the values
15971603
from the tuple.
15981604
1599-
over tuple of :mpltype:`color`, default: None
1600-
If tuple, the `over` value of each component is set with the values
1605+
over : tuple of :mpltype:`color`, default: None
1606+
If tuple, the ``over`` value of each component is set with the values
16011607
from the tuple.
16021608
16031609
Returns
@@ -1697,7 +1703,7 @@ def __init__(self, N=256, M=256, shape='square', origin=(0, 0),
16971703
self._rgba_bad = (0.0, 0.0, 0.0, 0.0) # If bad, don't paint anything.
16981704
self._rgba_outside = (1.0, 0.0, 1.0, 1.0)
16991705
self._isinit = False
1700-
self.n_variates = 2
1706+
self.n_components = 2
17011707
self._origin = (float(origin[0]), float(origin[1]))
17021708
'''#: When this colormap exists on a scalar mappable and colorbar_extend
17031709
#: is not False, colorbar creation will pick up ``colorbar_extend`` as
@@ -1709,7 +1715,8 @@ def __call__(self, X, alpha=None, bytes=False):
17091715
r"""
17101716
Parameters
17111717
----------
1712-
X : tuple (X0, X1), X0 and X1: float or int or array-like
1718+
X : tuple (X0, X1) or structured array with two fields, X0 and X1:
1719+
Xi: float or int or array-like.
17131720
The data value(s) to convert to RGBA.
17141721
17151722
- For floats, *X* should be in the interval ``[0.0, 1.0]`` to
@@ -1731,10 +1738,14 @@ def __call__(self, X, alpha=None, bytes=False):
17311738
RGBA values with a shape of ``X.shape + (4, )``.
17321739
"""
17331740

1741+
# unwrap structured data.
1742+
if isinstance(X, np.ndarray) and X.dtype.fields is not None:
1743+
X = tuple(X[descriptor[0]] for descriptor in X.dtype.descr)
1744+
17341745
if len(X) != 2:
17351746
raise ValueError(
1736-
f'For a `BivarColormap` the data must have a first dimension '
1737-
f'2, not {len(X)}')
1747+
'For a `BivarColormap` the data must have a first dimension '
1748+
f'2, not {len(X)}, or be a structured array with 2 fields.')
17381749

17391750
if not self._isinit:
17401751
self._init()
@@ -2264,8 +2275,8 @@ class Norm(ABC):
22642275
22652276
Subclasses include `Normalize` which maps from a scalar to
22662277
a scalar. However, this class makes no such requirement, and subclasses may
2267-
support the normalization of multiple variates simultaneously, with
2268-
separate normalization for each variate.
2278+
support the normalization of multiple components simultaneously, with
2279+
separate normalization for each component.
22692280
"""
22702281

22712282
def __init__(self):

lib/matplotlib/colors.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class ListedColormap(Colormap):
157157

158158
class MultivarColormap:
159159
name: str
160-
n_variates: int
160+
n_components: int
161161
def __init__(self, colormaps: list[Colormap], combination_mode: Literal['sRGB_add', 'sRGB_sub'], name: str = ...) -> None: ...
162162
@overload
163163
def __call__(
@@ -195,7 +195,7 @@ class BivarColormap:
195195
name: str
196196
N: int
197197
M: int
198-
n_variates: int
198+
n_components: int
199199
def __init__(
200200
self, N: int = ..., M: int | None = ..., shape: Literal['square', 'circle', 'ignore', 'circleignore'] = ...,
201201
origin: Sequence[float] = ..., name: str = ...

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy