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

Skip to content

Commit a833d99

Browse files
authored
Merge pull request #27001 from n-takumasa/fix-subplots-typing
[TYP] Add overload of `pyplot.subplots`
2 parents 394761a + 724696f commit a833d99

File tree

3 files changed

+76
-11
lines changed

3 files changed

+76
-11
lines changed

lib/matplotlib/figure.pyi

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from collections.abc import Callable, Hashable, Iterable
22
import os
3-
from typing import Any, IO, Literal, TypeVar, overload
3+
from typing import Any, IO, Literal, Sequence, TypeVar, overload
44

55
import numpy as np
66
from numpy.typing import ArrayLike
77

88
from matplotlib.artist import Artist
9-
from matplotlib.axes import Axes, SubplotBase
9+
from matplotlib.axes import Axes
1010
from matplotlib.backend_bases import (
1111
FigureCanvasBase,
1212
MouseButton,
@@ -92,6 +92,20 @@ class FigureBase(Artist):
9292
@overload
9393
def add_subplot(self, **kwargs) -> Axes: ...
9494
@overload
95+
def subplots(
96+
self,
97+
nrows: Literal[1] = ...,
98+
ncols: Literal[1] = ...,
99+
*,
100+
sharex: bool | Literal["none", "all", "row", "col"] = ...,
101+
sharey: bool | Literal["none", "all", "row", "col"] = ...,
102+
squeeze: Literal[True] = ...,
103+
width_ratios: Sequence[float] | None = ...,
104+
height_ratios: Sequence[float] | None = ...,
105+
subplot_kw: dict[str, Any] | None = ...,
106+
gridspec_kw: dict[str, Any] | None = ...,
107+
) -> Axes: ...
108+
@overload
95109
def subplots(
96110
self,
97111
nrows: int = ...,
@@ -100,11 +114,11 @@ class FigureBase(Artist):
100114
sharex: bool | Literal["none", "all", "row", "col"] = ...,
101115
sharey: bool | Literal["none", "all", "row", "col"] = ...,
102116
squeeze: Literal[False],
103-
width_ratios: ArrayLike | None = ...,
104-
height_ratios: ArrayLike | None = ...,
117+
width_ratios: Sequence[float] | None = ...,
118+
height_ratios: Sequence[float] | None = ...,
105119
subplot_kw: dict[str, Any] | None = ...,
106-
gridspec_kw: dict[str, Any] | None = ...
107-
) -> np.ndarray: ...
120+
gridspec_kw: dict[str, Any] | None = ...,
121+
) -> np.ndarray: ... # TODO numpy/numpy#24738
108122
@overload
109123
def subplots(
110124
self,
@@ -114,11 +128,11 @@ class FigureBase(Artist):
114128
sharex: bool | Literal["none", "all", "row", "col"] = ...,
115129
sharey: bool | Literal["none", "all", "row", "col"] = ...,
116130
squeeze: bool = ...,
117-
width_ratios: ArrayLike | None = ...,
118-
height_ratios: ArrayLike | None = ...,
131+
width_ratios: Sequence[float] | None = ...,
132+
height_ratios: Sequence[float] | None = ...,
119133
subplot_kw: dict[str, Any] | None = ...,
120-
gridspec_kw: dict[str, Any] | None = ...
121-
) -> np.ndarray | SubplotBase | Axes: ...
134+
gridspec_kw: dict[str, Any] | None = ...,
135+
) -> Axes | np.ndarray: ...
122136
def delaxes(self, ax: Axes) -> None: ...
123137
def clear(self, keep_observers: bool = ...) -> None: ...
124138
def clf(self, keep_observers: bool = ...) -> None: ...

lib/matplotlib/gridspec.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class GridSpecBase:
5454
sharey: bool | Literal["all", "row", "col", "none"] = ...,
5555
squeeze: Literal[True] = ...,
5656
subplot_kw: dict[str, Any] | None = ...
57-
) -> np.ndarray | SubplotBase | Axes: ...
57+
) -> np.ndarray | Axes: ...
5858

5959
class GridSpec(GridSpecBase):
6060
left: float | None

lib/matplotlib/pyplot.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,57 @@ def subplot(*args, **kwargs) -> Axes:
15601560
return ax
15611561

15621562

1563+
@overload
1564+
def subplots(
1565+
nrows: Literal[1] = ...,
1566+
ncols: Literal[1] = ...,
1567+
*,
1568+
sharex: bool | Literal["none", "all", "row", "col"] = ...,
1569+
sharey: bool | Literal["none", "all", "row", "col"] = ...,
1570+
squeeze: Literal[True] = ...,
1571+
width_ratios: Sequence[float] | None = ...,
1572+
height_ratios: Sequence[float] | None = ...,
1573+
subplot_kw: dict[str, Any] | None = ...,
1574+
gridspec_kw: dict[str, Any] | None = ...,
1575+
**fig_kw
1576+
) -> tuple[Figure, Axes]:
1577+
...
1578+
1579+
1580+
@overload
1581+
def subplots(
1582+
nrows: int = ...,
1583+
ncols: int = ...,
1584+
*,
1585+
sharex: bool | Literal["none", "all", "row", "col"] = ...,
1586+
sharey: bool | Literal["none", "all", "row", "col"] = ...,
1587+
squeeze: Literal[False],
1588+
width_ratios: Sequence[float] | None = ...,
1589+
height_ratios: Sequence[float] | None = ...,
1590+
subplot_kw: dict[str, Any] | None = ...,
1591+
gridspec_kw: dict[str, Any] | None = ...,
1592+
**fig_kw
1593+
) -> tuple[Figure, np.ndarray]: # TODO numpy/numpy#24738
1594+
...
1595+
1596+
1597+
@overload
1598+
def subplots(
1599+
nrows: int = ...,
1600+
ncols: int = ...,
1601+
*,
1602+
sharex: bool | Literal["none", "all", "row", "col"] = ...,
1603+
sharey: bool | Literal["none", "all", "row", "col"] = ...,
1604+
squeeze: bool = ...,
1605+
width_ratios: Sequence[float] | None = ...,
1606+
height_ratios: Sequence[float] | None = ...,
1607+
subplot_kw: dict[str, Any] | None = ...,
1608+
gridspec_kw: dict[str, Any] | None = ...,
1609+
**fig_kw
1610+
) -> tuple[Figure, Axes | np.ndarray]:
1611+
...
1612+
1613+
15631614
def subplots(
15641615
nrows: int = 1, ncols: int = 1, *,
15651616
sharex: bool | Literal["none", "all", "row", "col"] = False,

0 commit comments

Comments
 (0)