Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b570ecc

Browse files
committed
improved consistency of return types for better typing
1 parent 0a1597b commit b570ecc

4 files changed

Lines changed: 54 additions & 22 deletions

File tree

lib/matplotlib/colorizer.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,10 @@ def get_clim(self):
278278
"""
279279
Return the values (min, max) that are mapped to the colormap limits.
280280
"""
281-
return self.norm.vmin, self.norm.vmax
281+
if self.norm.n_components == 1:
282+
return (self.norm.vmin, ), (self.norm.vmax, )
283+
else:
284+
return self.norm.vmin, self.norm.vmax
282285

283286
def changed(self):
284287
"""
@@ -306,7 +309,10 @@ def vmax(self, vmax):
306309

307310
@property
308311
def clip(self):
309-
return self.norm.clip
312+
if self.norm.n_components == 1:
313+
return (self.norm.clip, )
314+
else:
315+
return self.norm.clip
310316

311317
@clip.setter
312318
def clip(self, clip):
@@ -360,8 +366,14 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True):
360366
def get_clim(self):
361367
"""
362368
Return the values (min, max) that are mapped to the colormap limits.
369+
370+
This function is not available for multivariate data.
363371
"""
364-
return self._colorizer.get_clim()
372+
if self._colorizer.norm.n_components > 1:
373+
raise AttributeError("`.get_clim()` is unavailable when using a colormap "
374+
"with multiple components. Use "
375+
"`.colorizer.get_clim()` instead")
376+
return self.colorizer.norm.vmin, self.colorizer.norm.vmax
365377

366378
def set_clim(self, vmin=None, vmax=None):
367379
"""
@@ -376,9 +388,15 @@ def set_clim(self, vmin=None, vmax=None):
376388
tuple (*vmin*, *vmax*) as a single positional argument.
377389
378390
.. ACCEPTS: (vmin: float, vmax: float)
391+
392+
This function is not available for multivariate data.
379393
"""
380394
# If the norm's limits are updated self.changed() will be called
381395
# through the callbacks attached to the norm
396+
if self._colorizer.norm.n_components > 1:
397+
raise AttributeError("`.set_clim(vmin, vmax)` is unavailable "
398+
"when using a colormap with multiple components. Use "
399+
"`.colorizer.set_clim(vmin, vmax)` instead")
382400
self._colorizer.set_clim(vmin, vmax)
383401

384402
def get_alpha(self):

lib/matplotlib/colorizer.pyi

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Colorizer:
1515
@property
1616
def norm(self) -> colors.Norm: ...
1717
@norm.setter
18-
def norm(self, norm: colors.Norm | str | tuple[str] | None) -> None: ...
18+
def norm(self, norm: colors.Norm | str | tuple[str, ...] | None) -> None: ...
1919
def to_rgba(
2020
self,
2121
x: np.ndarray,
@@ -29,21 +29,21 @@ class Colorizer:
2929
def cmap(self) -> colors.Colormap | colors.BivarColormap | colors.MultivarColormap: ...
3030
@cmap.setter
3131
def cmap(self, cmap: colors.Colormap | colors.BivarColormap | colors.MultivarColormap | str | None) -> None: ...
32-
def get_clim(self) -> tuple[float, float]: ...
33-
def set_clim(self, vmin: float | tuple[float, float] | None = ..., vmax: float | None = ...) -> None: ...
32+
def get_clim(self) -> tuple[tuple[float | None, ...], tuple[float | None, ...]]: ...
33+
def set_clim(self, vmin: float | tuple[float | None, ...] | None = ..., vmax: float | tuple[float | None, ...] | None = ...) -> None: ...
3434
def changed(self) -> None: ...
3535
@property
36-
def vmin(self) -> float | tuple[float] | None: ...
36+
def vmin(self) -> tuple[float | None, ...] | None: ...
3737
@vmin.setter
38-
def vmin(self, value: float | tuple[float] | None) -> None: ...
38+
def vmin(self, value: tuple[float | None, ...] | None) -> None: ...
3939
@property
40-
def vmax(self) -> float | tuple[float] | None: ...
40+
def vmax(self) -> tuple[float | None, ...] | None: ...
4141
@vmax.setter
42-
def vmax(self, value: float | tuple[float] | None) -> None: ...
42+
def vmax(self, value: tuple[float | None, ...] | None) -> None: ...
4343
@property
44-
def clip(self) -> bool | tuple[bool, ...]: ...
44+
def clip(self) -> tuple[bool, ...]: ...
4545
@clip.setter
46-
def clip(self, value: ArrayLike | bool) -> None: ...
46+
def clip(self, value: ArrayLike | bool | tuple[bool, ...]) -> None: ...
4747

4848

4949
class _ColorizerInterface:
@@ -57,8 +57,8 @@ class _ColorizerInterface:
5757
bytes: bool = ...,
5858
norm: bool = ...,
5959
) -> np.ndarray: ...
60-
def get_clim(self) -> tuple[float, float]: ...
61-
def set_clim(self, vmin: float | tuple[float, float] | None = ..., vmax: float | None = ...) -> None: ...
60+
def get_clim(self) -> tuple[float, float] | tuple[tuple[float, ...], tuple[float, ...]]: ...
61+
def set_clim(self, vmin: float | tuple[float, float] | tuple[float | None, ...] | None, vmax: float | tuple[float | None, ...] | None = ...) -> None: ...
6262
def get_alpha(self) -> float | None: ...
6363
def get_cmap(self) -> colors.Colormap | colors.BivarColormap | colors.MultivarColormap: ...
6464
def set_cmap(self, cmap: str | colors.Colormap | colors.BivarColormap | colors.MultivarColormap) -> None: ...

lib/matplotlib/colors.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,13 @@ class Norm(ABC):
255255
def __init__(self) -> None: ...
256256
@property
257257
@abstractmethod
258-
def vmin(self) -> float | tuple[float] | None: ...
258+
def vmin(self) -> float | tuple[float | None, ...] | None: ...
259259
@property
260260
@abstractmethod
261-
def vmax(self) -> float | tuple[float] | None: ...
261+
def vmax(self) -> float | tuple[float | None, ...] | None: ...
262262
@property
263263
@abstractmethod
264-
def clip(self) -> bool | tuple[bool]: ...
264+
def clip(self) -> bool | tuple[bool, ...]: ...
265265
@abstractmethod
266266
def __call__(self, value: np.ndarray, clip: bool | None = ...) -> ArrayLike: ...
267267
@abstractmethod

lib/matplotlib/tests/test_colors.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,16 +1786,30 @@ def test_is_color_like(input, expected):
17861786
assert is_color_like(input) is expected
17871787

17881788

1789-
def test_colorizer_vmin_vmax():
1789+
def test_colorizer_vmin_vmax_clip():
17901790
ca = mcolorizer.Colorizer()
1791-
assert ca.vmin is None
1792-
assert ca.vmax is None
1791+
assert len(ca.vmin) == 1
1792+
assert len(ca.vmax) == 1
1793+
assert ca.vmin[0] is None
1794+
assert ca.vmax[0] is None
17931795
ca.vmin = 1
17941796
ca.vmax = 3
1795-
assert ca.vmin == 1.0
1796-
assert ca.vmax == 3.0
1797+
assert ca.vmin == (1.0, )
1798+
assert ca.vmax == (3.0, )
17971799
assert ca.norm.vmin == 1.0
17981800
assert ca.norm.vmax == 3.0
1801+
assert ca.clip == (False, )
1802+
1803+
ca = mcolorizer.Colorizer('BiOrangeBlue')
1804+
assert len(ca.vmin) == 2
1805+
assert len(ca.vmax) == 2
1806+
ca.vmin = (1, 2)
1807+
ca.vmax = (3, 4)
1808+
assert ca.vmin == (1.0, 2.0)
1809+
assert ca.vmax == (3.0, 4.0)
1810+
assert ca.norm.vmin == (1.0, 2.0)
1811+
assert ca.norm.vmax == (3.0, 4.0)
1812+
assert ca.clip == (False, False)
17991813

18001814

18011815
def test_LinearSegmentedColormap_from_list_color_alpha_tuple():

0 commit comments

Comments
 (0)