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

Skip to content

Commit ccc61cb

Browse files
ksundenmeeseeksmachine
authored andcommitted
Backport PR #27001: [TYP] Add overload of pyplot.subplots
1 parent 5e0b83f commit ccc61cb

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
@@ -1548,6 +1548,57 @@ def subplot(*args, **kwargs) -> Axes:
15481548
return ax
15491549

15501550

1551+
@overload
1552+
def subplots(
1553+
nrows: Literal[1] = ...,
1554+
ncols: Literal[1] = ...,
1555+
*,
1556+
sharex: bool | Literal["none", "all", "row", "col"] = ...,
1557+
sharey: bool | Literal["none", "all", "row", "col"] = ...,
1558+
squeeze: Literal[True] = ...,
1559+
width_ratios: Sequence[float] | None = ...,
1560+
height_ratios: Sequence[float] | None = ...,
1561+
subplot_kw: dict[str, Any] | None = ...,
1562+
gridspec_kw: dict[str, Any] | None = ...,
1563+
**fig_kw
1564+
) -> tuple[Figure, Axes]:
1565+
...
1566+
1567+
1568+
@overload
1569+
def subplots(
1570+
nrows: int = ...,
1571+
ncols: int = ...,
1572+
*,
1573+
sharex: bool | Literal["none", "all", "row", "col"] = ...,
1574+
sharey: bool | Literal["none", "all", "row", "col"] = ...,
1575+
squeeze: Literal[False],
1576+
width_ratios: Sequence[float] | None = ...,
1577+
height_ratios: Sequence[float] | None = ...,
1578+
subplot_kw: dict[str, Any] | None = ...,
1579+
gridspec_kw: dict[str, Any] | None = ...,
1580+
**fig_kw
1581+
) -> tuple[Figure, np.ndarray]: # TODO numpy/numpy#24738
1582+
...
1583+
1584+
1585+
@overload
1586+
def subplots(
1587+
nrows: int = ...,
1588+
ncols: int = ...,
1589+
*,
1590+
sharex: bool | Literal["none", "all", "row", "col"] = ...,
1591+
sharey: bool | Literal["none", "all", "row", "col"] = ...,
1592+
squeeze: bool = ...,
1593+
width_ratios: Sequence[float] | None = ...,
1594+
height_ratios: Sequence[float] | None = ...,
1595+
subplot_kw: dict[str, Any] | None = ...,
1596+
gridspec_kw: dict[str, Any] | None = ...,
1597+
**fig_kw
1598+
) -> tuple[Figure, Axes | np.ndarray]:
1599+
...
1600+
1601+
15511602
def subplots(
15521603
nrows: int = 1, ncols: int = 1, *,
15531604
sharex: bool | Literal["none", "all", "row", "col"] = False,

0 commit comments

Comments
 (0)