Skip to content

[TYP] Add overload of pyplot.subplots #27001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions lib/matplotlib/figure.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from collections.abc import Callable, Hashable, Iterable
import os
from typing import Any, IO, Literal, TypeVar, overload
from typing import Any, IO, Literal, Sequence, TypeVar, overload

import numpy as np
from numpy.typing import ArrayLike

from matplotlib.artist import Artist
from matplotlib.axes import Axes, SubplotBase
from matplotlib.axes import Axes
from matplotlib.backend_bases import (
FigureCanvasBase,
MouseButton,
Expand Down Expand Up @@ -92,6 +92,20 @@ class FigureBase(Artist):
@overload
def add_subplot(self, **kwargs) -> Axes: ...
@overload
def subplots(
self,
nrows: Literal[1] = ...,
ncols: Literal[1] = ...,
*,
sharex: bool | Literal["none", "all", "row", "col"] = ...,
sharey: bool | Literal["none", "all", "row", "col"] = ...,
squeeze: Literal[True] = ...,
width_ratios: Sequence[float] | None = ...,
height_ratios: Sequence[float] | None = ...,
subplot_kw: dict[str, Any] | None = ...,
gridspec_kw: dict[str, Any] | None = ...,
) -> Axes: ...
@overload
def subplots(
self,
nrows: int = ...,
Expand All @@ -100,11 +114,11 @@ class FigureBase(Artist):
sharex: bool | Literal["none", "all", "row", "col"] = ...,
sharey: bool | Literal["none", "all", "row", "col"] = ...,
squeeze: Literal[False],
width_ratios: ArrayLike | None = ...,
height_ratios: ArrayLike | None = ...,
width_ratios: Sequence[float] | None = ...,
height_ratios: Sequence[float] | None = ...,
subplot_kw: dict[str, Any] | None = ...,
gridspec_kw: dict[str, Any] | None = ...
) -> np.ndarray: ...
gridspec_kw: dict[str, Any] | None = ...,
) -> np.ndarray: ... # TODO numpy/numpy#24738
@overload
def subplots(
self,
Expand All @@ -114,11 +128,11 @@ class FigureBase(Artist):
sharex: bool | Literal["none", "all", "row", "col"] = ...,
sharey: bool | Literal["none", "all", "row", "col"] = ...,
squeeze: bool = ...,
width_ratios: ArrayLike | None = ...,
height_ratios: ArrayLike | None = ...,
width_ratios: Sequence[float] | None = ...,
height_ratios: Sequence[float] | None = ...,
subplot_kw: dict[str, Any] | None = ...,
gridspec_kw: dict[str, Any] | None = ...
) -> np.ndarray | SubplotBase | Axes: ...
gridspec_kw: dict[str, Any] | None = ...,
) -> Axes | np.ndarray: ...
def delaxes(self, ax: Axes) -> None: ...
def clear(self, keep_observers: bool = ...) -> None: ...
def clf(self, keep_observers: bool = ...) -> None: ...
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/gridspec.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class GridSpecBase:
sharey: bool | Literal["all", "row", "col", "none"] = ...,
squeeze: Literal[True] = ...,
subplot_kw: dict[str, Any] | None = ...
) -> np.ndarray | SubplotBase | Axes: ...
) -> np.ndarray | Axes: ...

class GridSpec(GridSpecBase):
left: float | None
Expand Down
51 changes: 51 additions & 0 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,57 @@ def subplot(*args, **kwargs) -> Axes:
return ax


@overload
def subplots(
nrows: Literal[1] = ...,
ncols: Literal[1] = ...,
*,
sharex: bool | Literal["none", "all", "row", "col"] = ...,
sharey: bool | Literal["none", "all", "row", "col"] = ...,
squeeze: Literal[True] = ...,
width_ratios: Sequence[float] | None = ...,
height_ratios: Sequence[float] | None = ...,
subplot_kw: dict[str, Any] | None = ...,
gridspec_kw: dict[str, Any] | None = ...,
**fig_kw
) -> tuple[Figure, Axes]:
...


@overload
def subplots(
nrows: int = ...,
ncols: int = ...,
*,
sharex: bool | Literal["none", "all", "row", "col"] = ...,
sharey: bool | Literal["none", "all", "row", "col"] = ...,
squeeze: Literal[False],
width_ratios: Sequence[float] | None = ...,
height_ratios: Sequence[float] | None = ...,
subplot_kw: dict[str, Any] | None = ...,
gridspec_kw: dict[str, Any] | None = ...,
**fig_kw
) -> tuple[Figure, np.ndarray]: # TODO numpy/numpy#24738
...


@overload
def subplots(
nrows: int = ...,
ncols: int = ...,
*,
sharex: bool | Literal["none", "all", "row", "col"] = ...,
sharey: bool | Literal["none", "all", "row", "col"] = ...,
squeeze: bool = ...,
width_ratios: Sequence[float] | None = ...,
height_ratios: Sequence[float] | None = ...,
subplot_kw: dict[str, Any] | None = ...,
gridspec_kw: dict[str, Any] | None = ...,
**fig_kw
) -> tuple[Figure, Axes | np.ndarray]:
...


def subplots(
nrows: int = 1, ncols: int = 1, *,
sharex: bool | Literal["none", "all", "row", "col"] = False,
Expand Down
Loading