Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

ENH: Type the possible str legend locs as Literals#29465

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
timhoffm wants to merge1 commit intomatplotlib:main
base:main
Choose a base branch
Loading
fromtimhoffm:type-legend-loc
Open
Show file tree
Hide file tree
Changes fromall 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
13 changes: 8 additions & 5 deletionslib/matplotlib/axes/_axes.pyi
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,7 +38,7 @@ from collections.abc import Callable, Iterable, Sequence
from typing import Any, Literal, overload
import numpy as np
from numpy.typing import ArrayLike
from matplotlib.typing import ColorType, MarkerType, LineStyleType
from matplotlib.typing import ColorType, MarkerType,LegendLocType,LineStyleType

class Axes(_AxesBase):
def get_title(self, loc: Literal["left", "center", "right"] = ...) -> str: ...
Expand All@@ -60,13 +60,16 @@ class Axes(_AxesBase):
@overload
def legend(self) -> Legend: ...
@overload
def legend(self, handles: Iterable[Artist | tuple[Artist, ...]], labels: Iterable[str], **kwargs) -> Legend: ...
def legend(self, handles: Iterable[Artist | tuple[Artist, ...]], labels: Iterable[str],
*, loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I hope this works. I've pulledloc out of kwargs only for the stub; the method signature still does not contain it and handles it viakwargs instead. At least mypy doesn't complain, but I don't understand typing well enough whether that's guaranteed to work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

It should be fine since we haven't typedkwargs, I think.

@overload
def legend(self, *, handles: Iterable[Artist | tuple[Artist, ...]], **kwargs) -> Legend: ...
def legend(self, *, handles: Iterable[Artist | tuple[Artist, ...]],
loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
@overload
def legend(self, labels: Iterable[str], **kwargs) -> Legend: ...
def legend(self, labels: Iterable[str],
*, loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
@overload
def legend(self, **kwargs) -> Legend: ...
def legend(self, *, loc: LegendLocType | None = ..., **kwargs) -> Legend: ...

def inset_axes(
self,
Expand Down
13 changes: 8 additions & 5 deletionslib/matplotlib/figure.pyi
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,7 +25,7 @@ from matplotlib.lines import Line2D
from matplotlib.patches import Rectangle, Patch
from matplotlib.text import Text
from matplotlib.transforms import Affine2D, Bbox, BboxBase, Transform
from .typing import ColorType, HashableList
from .typing import ColorType, HashableList, LegendLocType

_T = TypeVar("_T")

Expand DownExpand Up@@ -147,13 +147,16 @@ class FigureBase(Artist):
@overload
def legend(self) -> Legend: ...
@overload
def legend(self, handles: Iterable[Artist], labels: Iterable[str], **kwargs) -> Legend: ...
def legend(self, handles: Iterable[Artist], labels: Iterable[str],
*, loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
@overload
def legend(self, *, handles: Iterable[Artist], **kwargs) -> Legend: ...
def legend(self, *, handles: Iterable[Artist],
loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
@overload
def legend(self, labels: Iterable[str], **kwargs) -> Legend: ...
def legend(self, labels: Iterable[str],
*, loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
@overload
def legend(self, **kwargs) -> Legend: ...
def legend(self, *, loc: LegendLocType | None = ..., **kwargs) -> Legend: ...

def text(
self,
Expand Down
7 changes: 4 additions & 3 deletionslib/matplotlib/legend.pyi
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,12 +14,13 @@ from matplotlib.transforms import (
BboxBase,
Transform,
)
from matplotlib.typing import ColorType, LegendLocType


import pathlib
from collections.abc import Iterable
from typing import Any, Literal, overload
from .typing import ColorType


class DraggableLegend(DraggableOffsetBox):
legend: Legend
Expand DownExpand Up@@ -55,7 +56,7 @@ class Legend(Artist):
handles: Iterable[Artist | tuple[Artist, ...]],
labels: Iterable[str],
*,
loc:str | tuple[float, float] | int | None = ...,
loc:LegendLocType | None = ...,
numpoints: int | None = ...,
markerscale: float | None = ...,
markerfirst: bool = ...,
Expand DownExpand Up@@ -118,7 +119,7 @@ class Legend(Artist):
def get_texts(self) -> list[Text]: ...
def set_alignment(self, alignment: Literal["center", "left", "right"]) -> None: ...
def get_alignment(self) -> Literal["center", "left", "right"]: ...
def set_loc(self, loc:str | tuple[float, float] | int | None = ...) -> None: ...
def set_loc(self, loc:LegendLocType | None = ...) -> None: ...
def set_title(
self, title: str, prop: FontProperties | str | pathlib.Path | None = ...
) -> None: ...
Expand Down
21 changes: 21 additions & 0 deletionslib/matplotlib/typing.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -80,3 +80,24 @@
_HT = TypeVar("_HT", bound=Hashable)
HashableList: TypeAlias = list[_HT | "HashableList[_HT]"]
"""A nested list of Hashable values."""


LegendLocType: TypeAlias = (
Literal[
# for simplicity, we don't distinguish the between allowed positions for
# Axes legend and figure legend. It's still better to limit the allowed
# range to the union of both rather than to accept arbitrary strings
"upper right", "upper left", "lower left", "lower right",
"right", "center left", "center right", "lower center", "upper center",
"center",
# Axes only
"best",
# Figure only
"outside upper left", "outside upper center", "outside upper right",
"outside right upper", "outside right center", "outside right lower",
"outside lower right", "outside lower center", "outside lower left",
"outside left lower", "outside left center", "outside left upper",
] |
tuple[float, float] |
int
)
Loading

[8]ページ先頭

©2009-2025 Movatter.jp