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 f608a46

Browse filesBrowse files
committed
ENH: Type the possible str legend locs as Literals
Instead of accepting any str, we only accept as set of predefined literals. 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 abitrary strings.
1 parent 685ea2b commit f608a46
Copy full SHA for f608a46

File tree

4 files changed

+40
-10
lines changed
Filter options

4 files changed

+40
-10
lines changed

‎lib/matplotlib/axes/_axes.pyi

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.pyi
+8-2Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ from collections.abc import Callable, Iterable, Sequence
3838
from typing import Any, Literal, overload
3939
import numpy as np
4040
from numpy.typing import ArrayLike
41-
from matplotlib.typing import ColorType, MarkerType, LineStyleType
41+
from matplotlib.typing import ColorType, MarkerType, LegendLocType, LineStyleType
4242

4343
class Axes(_AxesBase):
4444
def get_title(self, loc: Literal["left", "center", "right"] = ...) -> str: ...
@@ -60,7 +60,13 @@ class Axes(_AxesBase):
6060
@overload
6161
def legend(self) -> Legend: ...
6262
@overload
63-
def legend(self, handles: Iterable[Artist | tuple[Artist, ...]], labels: Iterable[str], **kwargs) -> Legend: ...
63+
def legend(
64+
self,
65+
handles: Iterable[Artist | tuple[Artist, ...]],
66+
labels: Iterable[str],
67+
*,
68+
loc: LegendLocType | None = ...,
69+
**kwargs) -> Legend: ...
6470
@overload
6571
def legend(self, *, handles: Iterable[Artist | tuple[Artist, ...]], **kwargs) -> Legend: ...
6672
@overload

‎lib/matplotlib/figure.pyi

Copy file name to clipboardExpand all lines: lib/matplotlib/figure.pyi
+8-5Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ from matplotlib.lines import Line2D
2525
from matplotlib.patches import Rectangle, Patch
2626
from matplotlib.text import Text
2727
from matplotlib.transforms import Affine2D, Bbox, BboxBase, Transform
28-
from .typing import ColorType, HashableList
28+
from .typing import ColorType, HashableList, LegendLocType
2929

3030
_T = TypeVar("_T")
3131

@@ -147,13 +147,16 @@ class FigureBase(Artist):
147147
@overload
148148
def legend(self) -> Legend: ...
149149
@overload
150-
def legend(self, handles: Iterable[Artist], labels: Iterable[str], **kwargs) -> Legend: ...
150+
def legend(self, handles: Iterable[Artist], labels: Iterable[str],
151+
*, loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
151152
@overload
152-
def legend(self, *, handles: Iterable[Artist], **kwargs) -> Legend: ...
153+
def legend(self, *, handles: Iterable[Artist],
154+
loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
153155
@overload
154-
def legend(self, labels: Iterable[str], **kwargs) -> Legend: ...
156+
def legend(self, labels: Iterable[str],
157+
*, loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
155158
@overload
156-
def legend(self, **kwargs) -> Legend: ...
159+
def legend(self, *, loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
157160

158161
def text(
159162
self,

‎lib/matplotlib/legend.pyi

Copy file name to clipboardExpand all lines: lib/matplotlib/legend.pyi
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ from matplotlib.transforms import (
1919
import pathlib
2020
from collections.abc import Iterable
2121
from typing import Any, Literal, overload
22-
from .typing import ColorType
22+
from .typing import ColorType, LegendLocType
2323

2424
class DraggableLegend(DraggableOffsetBox):
2525
legend: Legend
@@ -55,7 +55,7 @@ class Legend(Artist):
5555
handles: Iterable[Artist | tuple[Artist, ...]],
5656
labels: Iterable[str],
5757
*,
58-
loc: str | tuple[float, float] | int | None = ...,
58+
loc: LegendLocType | None = ...,
5959
numpoints: int | None = ...,
6060
markerscale: float | None = ...,
6161
markerfirst: bool = ...,
@@ -118,7 +118,7 @@ class Legend(Artist):
118118
def get_texts(self) -> list[Text]: ...
119119
def set_alignment(self, alignment: Literal["center", "left", "right"]) -> None: ...
120120
def get_alignment(self) -> Literal["center", "left", "right"]: ...
121-
def set_loc(self, loc: str | tuple[float, float] | int | None = ...) -> None: ...
121+
def set_loc(self, loc: LegendLocType | None = ...) -> None: ...
122122
def set_title(
123123
self, title: str, prop: FontProperties | str | pathlib.Path | None = ...
124124
) -> None: ...

‎lib/matplotlib/typing.py

Copy file name to clipboardExpand all lines: lib/matplotlib/typing.py
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,24 @@
8080
_HT = TypeVar("_HT", bound=Hashable)
8181
HashableList: TypeAlias = list[_HT | "HashableList[_HT]"]
8282
"""A nested list of Hashable values."""
83+
84+
85+
LegendLocType: TypeAlias = (
86+
Literal[
87+
# for simplicity, we don't distinguish the between allowed positions for
88+
# Axes legend and figure legend. It's still better to limit the allowed
89+
# range to the union of both rather than to accept arbitrary strings
90+
"upper right", "upper left", "lower left", "lower right",
91+
"right", "center left", "center right", "lower center", "upper center",
92+
"center",
93+
# Axes only
94+
"best",
95+
# Figure only
96+
"outside upper left", "outside upper center", "outside upper right",
97+
"outside right upper", "outside right center", "outside right lower",
98+
"outside lower right", "outside lower center", "outside lower left",
99+
"outside left lower", "outside left center", "outside left upper",
100+
] |
101+
tuple[float, float] |
102+
int
103+
)

0 commit comments

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