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 631e38f

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 631e38f
Copy full SHA for 631e38f

File tree

Expand file treeCollapse file tree

4 files changed

+41
-13
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+41
-13
lines changed

‎lib/matplotlib/axes/_axes.pyi

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.pyi
+8-5Lines changed: 8 additions & 5 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,13 +60,16 @@ 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(self, handles: Iterable[Artist | tuple[Artist, ...]], labels: Iterable[str],
64+
*, loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
6465
@overload
65-
def legend(self, *, handles: Iterable[Artist | tuple[Artist, ...]], **kwargs) -> Legend: ...
66+
def legend(self, *, handles: Iterable[Artist | tuple[Artist, ...]],
67+
loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
6668
@overload
67-
def legend(self, labels: Iterable[str], **kwargs) -> Legend: ...
69+
def legend(self, labels: Iterable[str],
70+
*, loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
6871
@overload
69-
def legend(self, **kwargs) -> Legend: ...
72+
def legend(self, *, loc: LegendLocType | None = ..., **kwargs) -> Legend: ...
7073

7174
def inset_axes(
7275
self,

‎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
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ from matplotlib.transforms import (
1414
BboxBase,
1515
Transform,
1616
)
17+
from matplotlib.typing import ColorType, LegendLocType
1718

1819

1920
import pathlib
2021
from collections.abc import Iterable
2122
from typing import Any, Literal, overload
22-
from .typing import ColorType
23+
2324

2425
class DraggableLegend(DraggableOffsetBox):
2526
legend: Legend
@@ -55,7 +56,7 @@ class Legend(Artist):
5556
handles: Iterable[Artist | tuple[Artist, ...]],
5657
labels: Iterable[str],
5758
*,
58-
loc: str | tuple[float, float] | int | None = ...,
59+
loc: LegendLocType | None = ...,
5960
numpoints: int | None = ...,
6061
markerscale: float | None = ...,
6162
markerfirst: bool = ...,
@@ -118,7 +119,7 @@ class Legend(Artist):
118119
def get_texts(self) -> list[Text]: ...
119120
def set_alignment(self, alignment: Literal["center", "left", "right"]) -> None: ...
120121
def get_alignment(self) -> Literal["center", "left", "right"]: ...
121-
def set_loc(self, loc: str | tuple[float, float] | int | None = ...) -> None: ...
122+
def set_loc(self, loc: LegendLocType | None = ...) -> None: ...
122123
def set_title(
123124
self, title: str, prop: FontProperties | str | pathlib.Path | None = ...
124125
) -> 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.