Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 39243e2

Browse filesBrowse files
committed
typing for colorizer changes
1 parent 35aacdc commit 39243e2
Copy full SHA for 39243e2

17 files changed

+251
-103
lines changed

‎lib/matplotlib/axes/_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.py
+12-3Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import matplotlib.category # Register category unit converter as side effect.
1313
import matplotlib.cbook as cbook
1414
import matplotlib.collections as mcoll
15+
import matplotlib.colorizer as mcolorizer
1516
import matplotlib.colors as mcolors
1617
import matplotlib.contour as mcontour
1718
import matplotlib.dates # noqa: F401, Register date unit converter as side effect.
@@ -4955,8 +4956,9 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
49554956
collection.set_transform(mtransforms.IdentityTransform())
49564957
if colors is None:
49574958
if colorizer:
4958-
collection._set_colorizer_check_keywords(colorizer, cmap, norm,
4959-
vmin, vmax)
4959+
collection._set_colorizer_check_keywords(colorizer, cmap=cmap,
4960+
norm=norm, vmin=vmin,
4961+
vmax=vmax)
49604962
else:
49614963
collection.set_cmap(cmap)
49624964
collection.set_norm(norm)
@@ -5314,7 +5316,9 @@ def reduce_C_function(C: array) -> float
53145316
accum = bins.searchsorted(accum)
53155317

53165318
if colorizer:
5317-
collection._set_colorizer_check_keywords(colorizer, cmap, norm, vmin, vmax)
5319+
collection._set_colorizer_check_keywords(colorizer, cmap=cmap,
5320+
norm=norm, vmin=vmin,
5321+
vmax=vmax)
53185322
else:
53195323
collection.set_cmap(cmap)
53205324
collection.set_norm(norm)
@@ -5986,6 +5990,7 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None,
59865990
if im.get_clip_path() is None:
59875991
# image does not already have clipping set, clip to Axes patch
59885992
im.set_clip_path(self.patch)
5993+
im._check_exclusionary_keywords(colorizer, vmin=vmin, vmax=vmax)
59895994
im._scale_norm(norm, vmin, vmax)
59905995
im.set_url(url)
59915996

@@ -6300,6 +6305,7 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
63006305
collection = mcoll.PolyQuadMesh(
63016306
coords, array=C, cmap=cmap, norm=norm, colorizer=colorizer,
63026307
alpha=alpha, **kwargs)
6308+
collection._check_exclusionary_keywords(colorizer, vmin=vmin, vmax=vmax)
63036309
collection._scale_norm(norm, vmin, vmax)
63046310

63056311
# Transform from native to data coordinates?
@@ -6533,6 +6539,7 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
65336539
collection = mcoll.QuadMesh(
65346540
coords, antialiased=antialiased, shading=shading,
65356541
array=C, cmap=cmap, norm=norm, colorizer=colorizer, alpha=alpha, **kwargs)
6542+
collection._check_exclusionary_keywords(colorizer, vmin=vmin, vmax=vmax)
65366543
collection._scale_norm(norm, vmin, vmax)
65376544

65386545
coords = coords.reshape(-1, 2) # flatten the grid structure; keep x, y
@@ -6707,6 +6714,8 @@ def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
67076714
else:
67086715
raise _api.nargs_error('pcolorfast', '1 or 3', len(args))
67096716

6717+
mcolorizer.ColorizingArtist._check_exclusionary_keywords(colorizer, vmin=vmin,
6718+
vmax=vmax)
67106719
if style == "quadmesh":
67116720
# data point in each cell is value at lower left corner
67126721
coords = np.stack([x, y], axis=-1)

‎lib/matplotlib/axes/_axes.pyi

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.pyi
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ from matplotlib.collections import (
1111
EventCollection,
1212
QuadMesh,
1313
)
14+
from matplotlib.colorizer import Colorizer
1415
from matplotlib.colors import Colormap, Normalize
1516
from matplotlib.container import BarContainer, ErrorbarContainer, StemContainer
1617
from matplotlib.contour import ContourSet, QuadContourSet
@@ -415,6 +416,7 @@ class Axes(_AxesBase):
415416
norm: str | Normalize | None = ...,
416417
vmin: float | None = ...,
417418
vmax: float | None = ...,
419+
colorizer: Colorizer | None = ...,
418420
alpha: float | None = ...,
419421
linewidths: float | Sequence[float] | None = ...,
420422
edgecolors: Literal["face", "none"] | ColorType | Sequence[ColorType] | None = ...,
@@ -437,6 +439,7 @@ class Axes(_AxesBase):
437439
norm: str | Normalize | None = ...,
438440
vmin: float | None = ...,
439441
vmax: float | None = ...,
442+
colorizer: Colorizer | None = ...,
440443
alpha: float | None = ...,
441444
linewidths: float | None = ...,
442445
edgecolors: Literal["face", "none"] | ColorType = ...,
@@ -490,6 +493,7 @@ class Axes(_AxesBase):
490493
alpha: float | ArrayLike | None = ...,
491494
vmin: float | None = ...,
492495
vmax: float | None = ...,
496+
colorizer: Colorizer | None = ...,
493497
origin: Literal["upper", "lower"] | None = ...,
494498
extent: tuple[float, float, float, float] | None = ...,
495499
interpolation_stage: Literal["data", "rgba", "auto"] | None = ...,
@@ -509,6 +513,7 @@ class Axes(_AxesBase):
509513
cmap: str | Colormap | None = ...,
510514
vmin: float | None = ...,
511515
vmax: float | None = ...,
516+
colorizer: Colorizer | None = ...,
512517
data=...,
513518
**kwargs
514519
) -> Collection: ...
@@ -520,6 +525,7 @@ class Axes(_AxesBase):
520525
cmap: str | Colormap | None = ...,
521526
vmin: float | None = ...,
522527
vmax: float | None = ...,
528+
colorizer: Colorizer | None = ...,
523529
shading: Literal["flat", "nearest", "gouraud", "auto"] | None = ...,
524530
antialiased: bool = ...,
525531
data=...,
@@ -533,6 +539,7 @@ class Axes(_AxesBase):
533539
cmap: str | Colormap | None = ...,
534540
vmin: float | None = ...,
535541
vmax: float | None = ...,
542+
colorizer: Colorizer | None = ...,
536543
data=...,
537544
**kwargs
538545
) -> AxesImage | PcolorImage | QuadMesh: ...

‎lib/matplotlib/axes/_base.pyi

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_base.pyi
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ from matplotlib.backend_bases import RendererBase, MouseButton, MouseEvent
1010
from matplotlib.cbook import CallbackRegistry
1111
from matplotlib.container import Container
1212
from matplotlib.collections import Collection
13-
from matplotlib.cm import ScalarMappable
13+
from matplotlib.colorizer import ColorizingArtist
1414
from matplotlib.legend import Legend
1515
from matplotlib.lines import Line2D
1616
from matplotlib.gridspec import SubplotSpec, GridSpec
@@ -400,7 +400,7 @@ class _AxesBase(martist.Artist):
400400
def get_xticklines(self, minor: bool = ...) -> list[Line2D]: ...
401401
def get_ygridlines(self) -> list[Line2D]: ...
402402
def get_yticklines(self, minor: bool = ...) -> list[Line2D]: ...
403-
def _sci(self, im: ScalarMappable) -> None: ...
403+
def _sci(self, im: ColorizingArtist) -> None: ...
404404
def get_autoscalex_on(self) -> bool: ...
405405
def get_autoscaley_on(self) -> bool: ...
406406
def set_autoscalex_on(self, b: bool) -> None: ...

‎lib/matplotlib/cm.pyi

Copy file name to clipboard
+2-25Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from collections.abc import Iterator, Mapping
2-
from matplotlib import cbook, colors
3-
from matplotlib.colorbar import Colorbar
2+
from matplotlib import colors, colorizer
43

54
import numpy as np
65
from numpy.typing import ArrayLike
@@ -21,34 +20,12 @@ _colormaps: ColormapRegistry = ...
2120

2221
def get_cmap(name: str | colors.Colormap | None = ..., lut: int | None = ...) -> colors.Colormap: ...
2322

24-
class ScalarMappable:
25-
cmap: colors.Colormap | None
26-
colorbar: Colorbar | None
27-
callbacks: cbook.CallbackRegistry
23+
class ScalarMappable(colorizer._ColorizerInterface):
2824
def __init__(
2925
self,
3026
norm: colors.Normalize | None = ...,
3127
cmap: str | colors.Colormap | None = ...,
3228
) -> None: ...
33-
def to_rgba(
34-
self,
35-
x: np.ndarray,
36-
alpha: float | ArrayLike | None = ...,
37-
bytes: bool = ...,
38-
norm: bool = ...,
39-
) -> np.ndarray: ...
4029
def set_array(self, A: ArrayLike | None) -> None: ...
4130
def get_array(self) -> np.ndarray | None: ...
42-
def get_cmap(self) -> colors.Colormap: ...
43-
def get_clim(self) -> tuple[float, float]: ...
44-
def set_clim(self, vmin: float | tuple[float, float] | None = ..., vmax: float | None = ...) -> None: ...
45-
def get_alpha(self) -> float | None: ...
46-
def set_cmap(self, cmap: str | colors.Colormap) -> None: ...
47-
@property
48-
def norm(self) -> colors.Normalize: ...
49-
@norm.setter
50-
def norm(self, norm: colors.Normalize | str | None) -> None: ...
51-
def set_norm(self, norm: colors.Normalize | str | None) -> None: ...
52-
def autoscale(self) -> None: ...
53-
def autoscale_None(self) -> None: ...
5431
def changed(self) -> None: ...

‎lib/matplotlib/collections.pyi

Copy file name to clipboardExpand all lines: lib/matplotlib/collections.pyi
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ from typing import Literal
44
import numpy as np
55
from numpy.typing import ArrayLike, NDArray
66

7-
from . import artist, cm, transforms
7+
from . import colorizer, transforms
88
from .backend_bases import MouseEvent
99
from .artist import Artist
10+
from .colorizer import Colorizer
1011
from .colors import Normalize, Colormap
1112
from .lines import Line2D
1213
from .path import Path
@@ -15,7 +16,7 @@ from .ticker import Locator, Formatter
1516
from .tri import Triangulation
1617
from .typing import ColorType, LineStyleType, CapStyleType, JoinStyleType
1718

18-
class Collection(artist.Artist, cm.ScalarMappable):
19+
class Collection(colorizer.ColorizingArtist):
1920
def __init__(
2021
self,
2122
*,
@@ -30,6 +31,7 @@ class Collection(artist.Artist, cm.ScalarMappable):
3031
offset_transform: transforms.Transform | None = ...,
3132
norm: Normalize | None = ...,
3233
cmap: Colormap | None = ...,
34+
colorizer: Colorizer | None = ...,
3335
pickradius: float = ...,
3436
hatch: str | None = ...,
3537
urls: Sequence[str] | None = ...,

‎lib/matplotlib/colorbar.pyi

Copy file name to clipboardExpand all lines: lib/matplotlib/colorbar.pyi
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import matplotlib.spines as mspines
2-
from matplotlib import cm, collections, colors, contour
2+
from matplotlib import cm, collections, colors, contour, colorizer
33
from matplotlib.axes import Axes
44
from matplotlib.backend_bases import RendererBase
55
from matplotlib.patches import Patch
@@ -21,7 +21,7 @@ class _ColorbarSpine(mspines.Spines):
2121

2222
class Colorbar:
2323
n_rasterize: int
24-
mappable: cm.ScalarMappable
24+
mappable: cm.ScalarMappable | colorizer.ColorizingArtist
2525
ax: Axes
2626
alpha: float | None
2727
cmap: colors.Colormap
@@ -43,7 +43,7 @@ class Colorbar:
4343
def __init__(
4444
self,
4545
ax: Axes,
46-
mappable: cm.ScalarMappable | None = ...,
46+
mappable: cm.ScalarMappable | colorizer.ColorizingArtist | None = ...,
4747
*,
4848
cmap: str | colors.Colormap | None = ...,
4949
norm: colors.Normalize | None = ...,
@@ -78,7 +78,7 @@ class Colorbar:
7878
def minorformatter(self) -> Formatter: ...
7979
@minorformatter.setter
8080
def minorformatter(self, fmt: Formatter) -> None: ...
81-
def update_normal(self, mappable: cm.ScalarMappable) -> None: ...
81+
def update_normal(self, mappable: cm.ScalarMappable | None = ...) -> None: ...
8282
@overload
8383
def add_lines(self, CS: contour.ContourSet, erase: bool = ...) -> None: ...
8484
@overload

‎lib/matplotlib/colorizer.py

Copy file name to clipboardExpand all lines: lib/matplotlib/colorizer.py
+17-10Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,8 @@ def _get_colorizer(cmap, norm, colorizer):
315315
Passes or creates a Colorizer object.
316316
"""
317317
if colorizer and isinstance(colorizer, Colorizer):
318-
if cmap or norm:
319-
raise ValueError("Providing a `cm.Colorizer` while at the same time "
320-
"providing a `norm` or `cmap` is not supported.")
318+
ColorizingArtist._check_exclusionary_keywords(Colorizer,
319+
cmap=cmap, norm=norm)
321320
return colorizer
322321
return Colorizer(cmap, norm)
323322

@@ -578,17 +577,25 @@ def colorizer(self, cl):
578577
raise ValueError("colorizer must be a `Colorizer` object, not "
579578
f" {type(cl)}.")
580579

581-
def _set_colorizer_check_keywords(self, colorizer, cmap=None, norm=None,
582-
vmin=None, vmax=None):
580+
@staticmethod
581+
def _check_exclusionary_keywords(colorizer, **kwargs):
583582
"""
584-
Raises a ValueError on incompatible keyword combinations
583+
Raises a ValueError if any kwarg is not None while colorizer is not None
585584
"""
586585
if colorizer is not None:
587-
if cmap is None and norm is None and vmin is None and vmin is None:
588-
self.colorizer = colorizer
589-
else:
586+
if any([val is not None for val in kwargs.values()]):
590587
raise ValueError("The `colorizer` keyword cannot be used simultaneously"
591-
" with `cmap`, `norm`, `vmin` or `vmax`.")
588+
" with any of the following keywords: "
589+
+ ", ".join(f'`{key}`' for key in kwargs.keys()))
590+
591+
def _set_colorizer_check_keywords(self, colorizer, **kwargs):
592+
"""
593+
Raises a ValueError if any kwarg is not None while colorizer is not None
594+
595+
Then sets the colorizer.
596+
"""
597+
self._check_exclusionary_keywords(colorizer, **kwargs)
598+
self.colorizer = colorizer
592599

593600

594601
def _auto_norm_from_scale(scale_cls):

‎lib/matplotlib/colorizer.pyi

Copy file name to clipboard
+90Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from matplotlib import cbook, colorbar, colors, artist
2+
3+
from typing import overload
4+
import numpy as np
5+
from numpy.typing import ArrayLike
6+
7+
8+
class Colorizer():
9+
colorbar: colorbar.Colorbar | None
10+
callbacks: cbook.CallbackRegistry
11+
def __init__(
12+
self,
13+
norm: colors.Normalize | str | None = ...,
14+
cmap: str | colors.Colormap | None = ...,
15+
) -> None: ...
16+
@property
17+
def norm(self) -> colors.Normalize: ...
18+
@norm.setter
19+
def norm(self, norm: colors.Normalize | str | None) -> None: ...
20+
def to_rgba(
21+
self,
22+
x: np.ndarray,
23+
alpha: float | ArrayLike | None = ...,
24+
bytes: bool = ...,
25+
norm: bool = ...,
26+
) -> np.ndarray: ...
27+
@overload
28+
def normalize(self, value: float, clip: bool | None = ...) -> float: ...
29+
@overload
30+
def normalize(self, value: np.ndarray, clip: bool | None = ...) -> np.ma.MaskedArray: ...
31+
@overload
32+
def normalize(self, value: ArrayLike, clip: bool | None = ...) -> ArrayLike: ...
33+
def autoscale(self, A: ArrayLike) -> None: ...
34+
def autoscale_None(self, A: ArrayLike) -> None: ...
35+
@property
36+
def cmap(self) -> colors.Colormap: ...
37+
@cmap.setter
38+
def cmap(self, cmap: colors.Colormap | str | None) -> None: ...
39+
def get_clim(self) -> tuple[float, float]: ...
40+
def set_clim(self, vmin: float | tuple[float, float] | None = ..., vmax: float | None = ...) -> None: ...
41+
def changed(self) -> None: ...
42+
@property
43+
def vmin(self) -> float | None: ...
44+
@vmin.setter
45+
def vmin(self, value: float | None) -> None: ...
46+
@property
47+
def vmax(self) -> float | None: ...
48+
@vmax.setter
49+
def vmax(self, value: float | None) -> None: ...
50+
@property
51+
def clip(self) -> bool: ...
52+
@clip.setter
53+
def clip(self, value: bool) -> None: ...
54+
55+
56+
class _ColorizerInterface:
57+
cmap: colors.Colormap
58+
colorbar: colorbar.Colorbar | None
59+
callbacks: cbook.CallbackRegistry
60+
def to_rgba(
61+
self,
62+
x: np.ndarray,
63+
alpha: float | ArrayLike | None = ...,
64+
bytes: bool = ...,
65+
norm: bool = ...,
66+
) -> np.ndarray: ...
67+
def get_clim(self) -> tuple[float, float]: ...
68+
def set_clim(self, vmin: float | tuple[float, float] | None = ..., vmax: float | None = ...) -> None: ...
69+
def get_alpha(self) -> float | None: ...
70+
def get_cmap(self) -> colors.Colormap: ...
71+
def set_cmap(self, cmap: str | colors.Colormap) -> None: ...
72+
@property
73+
def norm(self) -> colors.Normalize: ...
74+
@norm.setter
75+
def norm(self, norm: colors.Normalize | str | None) -> None: ...
76+
def set_norm(self, norm: colors.Normalize | str | None) -> None: ...
77+
def autoscale(self) -> None: ...
78+
def autoscale_None(self) -> None: ...
79+
80+
81+
class ColorizingArtist(artist.Artist, _ColorizerInterface):
82+
callbacks: cbook.CallbackRegistry
83+
def __init__(
84+
self,
85+
norm: colors.Normalize | None = ...,
86+
cmap: str | colors.Colormap | None = ...,
87+
) -> None: ...
88+
def set_array(self, A: ArrayLike | None) -> None: ...
89+
def get_array(self) -> np.ndarray | None: ...
90+
def changed(self) -> None: ...

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.