Skip to content

Commit f42d65b

Browse files
trygvradQuLogic
andcommitted
Apply suggestions from code review
Thank you @QuLogic Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
1 parent 6985111 commit f42d65b

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

lib/matplotlib/colors.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3242,13 +3242,13 @@ def __init__(self, norms, vmin=None, vmax=None, clip=False):
32423242
The constituent norms. The list must have a minimum length of 2.
32433243
vmin, vmax : float, None, or list of float or None
32443244
Limits of the constituent norms.
3245-
If a list, each each value is assigned to one of the constituent
3245+
If a list, each value is assigned to each of the constituent
32463246
norms. Single values are repeated to form a list of appropriate size.
32473247
32483248
clip : bool or list of bools, default: False
32493249
Determines the behavior for mapping values outside the range
32503250
``[vmin, vmax]`` for the constituent norms.
3251-
If a list, each each value is assigned to one of the constituent
3251+
If a list, each value is assigned to each of the constituent
32523252
norms. Single values are repeated to form a list of appropriate size.
32533253
32543254
"""
@@ -3263,6 +3263,10 @@ def __init__(self, norms, vmin=None, vmax=None, clip=False):
32633263
elif isinstance(n, str):
32643264
scale_cls = _get_scale_cls_from_str(n)
32653265
norms[i] = mpl.colorizer._auto_norm_from_scale(scale_cls)()
3266+
elif not isinstance(n, Normalize):
3267+
raise ValueError(
3268+
"MultiNorm must be assigned multiple norms, where each norm "
3269+
f"is of type `None` `str`, or `Normalize`, not {type(n)}")
32663270

32673271
# Convert the list of norms to a tuple to make it immutable.
32683272
# If there is a use case for swapping a single norm, we can add support for
@@ -3275,8 +3279,7 @@ def __init__(self, norms, vmin=None, vmax=None, clip=False):
32753279
self.vmax = vmax
32763280
self.clip = clip
32773281

3278-
self._id_norms = [n.callbacks.connect('changed',
3279-
self._changed) for n in self._norms]
3282+
[n.callbacks.connect('changed', self._changed) for n in self._norms]
32803283

32813284
@property
32823285
def n_input(self):
@@ -3348,7 +3351,8 @@ def _changed(self):
33483351
def __call__(self, value, clip=None):
33493352
"""
33503353
Normalize the data and return the normalized data.
3351-
Each variate in the input is assigned to the a constituent norm.
3354+
3355+
Each variate in the input is assigned to the constituent norm.
33523356
33533357
Parameters
33543358
----------
@@ -3381,8 +3385,7 @@ def __call__(self, value, clip=None):
33813385

33823386
def inverse(self, value):
33833387
"""
3384-
Maps the normalized value (i.e., index in the colormap) back to image
3385-
data value.
3388+
Map the normalized value (i.e., index in the colormap) back to image data value.
33863389
33873390
Parameters
33883391
----------
@@ -3449,7 +3452,7 @@ def _iterable_variates_in_data(data, n_input):
34493452
"""
34503453
if isinstance(data, np.ndarray) and data.dtype.fields is not None:
34513454
data = [data[descriptor[0]] for descriptor in data.dtype.descr]
3452-
if not len(data) == n_input:
3455+
if len(data) != n_input:
34533456
raise ValueError("The input to this `MultiNorm` must be of shape "
34543457
f"({n_input}, ...), or have a data type with {n_input} "
34553458
"fields.")
@@ -4100,9 +4103,11 @@ def _get_scale_cls_from_str(scale_as_str):
41004103
Returns the scale class from a string.
41014104
41024105
Used in the creation of norms from a string to ensure a reasonable error
4103-
in the case where an invalid string is used. This cannot use
4104-
`_api.check_getitem()`, because the norm keyword accepts arguments
4105-
other than strings.
4106+
in the case where an invalid string is used. This would normally use
4107+
`_api.check_getitem()`, which would produce the error
4108+
> 'not_a_norm' is not a valid value for norm; supported values are
4109+
> 'linear', 'log', 'symlog', 'asinh', 'logit', 'function', 'functionlog'
4110+
which is misleading because the norm keyword also accepts `Normalize` objects.
41064111
41074112
Parameters
41084113
----------

lib/matplotlib/colors.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,17 +403,17 @@ class MultiNorm(Normalize):
403403
clip: ArrayLike | bool = ...
404404
) -> None: ...
405405
@property
406-
def norms(self) -> tuple: ...
406+
def norms(self) -> tuple[Normalize, ...]: ...
407407
@property # type: ignore[override]
408-
def vmin(self) -> tuple[float | None]: ...
408+
def vmin(self) -> tuple[float | None, ...]: ...
409409
@vmin.setter
410410
def vmin(self, value: ArrayLike | float | None) -> None: ...
411411
@property # type: ignore[override]
412-
def vmax(self) -> tuple[float | None]: ...
412+
def vmax(self) -> tuple[float | None, ...]: ...
413413
@vmax.setter
414414
def vmax(self, value: ArrayLike | float | None) -> None: ...
415415
@property # type: ignore[override]
416-
def clip(self) -> tuple[bool]: ...
416+
def clip(self) -> tuple[bool, ...]: ...
417417
@clip.setter
418418
def clip(self, value: ArrayLike | bool) -> None: ...
419419
def __call__(self, value: ArrayLike, clip: ArrayLike | bool | None = ...) -> list: ... # type: ignore[override]

lib/matplotlib/tests/test_colors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,6 +1840,10 @@ def test_multi_norm():
18401840
with pytest.raises(ValueError,
18411841
match="Invalid norm str name"):
18421842
mcolors.MultiNorm(["bad_norm_name"])
1843+
with pytest.raises(ValueError,
1844+
match="MultiNorm must be assigned multiple norms, "
1845+
"where each norm is of type `None`"):
1846+
mcolors.MultiNorm([4])
18431847

18441848
# test get vmin, vmax
18451849
norm = mpl.colors.MultiNorm(['linear', 'log'])

0 commit comments

Comments
 (0)