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

Commit92eba37

Browse files
committed
ENH: Type the possible str legend locs as Literals
Instead of accepting any str, we only accept as set of predefinedliterals. For simplicity, we don't distinguish the between allowedpositions for Axes legend and figure legend. It's still better to limitthe allowed range to the union of both rather than to accept abitrarystrings.
1 parent685ea2b commit92eba37

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

‎lib/matplotlib/axes/_axes.pyi

Lines 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
fromtypingimportAny,Literal,overload
3939
importnumpyasnp
4040
fromnumpy.typingimportArrayLike
41-
frommatplotlib.typingimportColorType,MarkerType,LineStyleType
41+
frommatplotlib.typingimportColorType,MarkerType,LegendLocType,LineStyleType
4242

4343
classAxes(_AxesBase):
4444
defget_title(self,loc:Literal["left","center","right"]= ...)->str: ...
@@ -60,13 +60,16 @@ class Axes(_AxesBase):
6060
@overload
6161
deflegend(self)->Legend: ...
6262
@overload
63-
deflegend(self,handles:Iterable[Artist|tuple[Artist, ...]],labels:Iterable[str],**kwargs)->Legend: ...
63+
deflegend(self,handles:Iterable[Artist|tuple[Artist, ...]],labels:Iterable[str],
64+
*,loc:LegendLocType|None= ...,**kwargs)->Legend: ...
6465
@overload
65-
deflegend(self,*,handles:Iterable[Artist|tuple[Artist, ...]],**kwargs)->Legend: ...
66+
deflegend(self,*,handles:Iterable[Artist|tuple[Artist, ...]],
67+
loc:LegendLocType|None= ...,**kwargs)->Legend: ...
6668
@overload
67-
deflegend(self,labels:Iterable[str],**kwargs)->Legend: ...
69+
deflegend(self,labels:Iterable[str],
70+
*,loc:LegendLocType|None= ...,**kwargs)->Legend: ...
6871
@overload
69-
deflegend(self,**kwargs)->Legend: ...
72+
deflegend(self,*,loc:LegendLocType|None= ...,**kwargs)->Legend: ...
7073

7174
definset_axes(
7275
self,

‎lib/matplotlib/figure.pyi

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ from matplotlib.lines import Line2D
2525
frommatplotlib.patchesimportRectangle,Patch
2626
frommatplotlib.textimportText
2727
frommatplotlib.transformsimportAffine2D,Bbox,BboxBase,Transform
28-
from .typingimportColorType,HashableList
28+
from .typingimportColorType,HashableList,LegendLocType
2929

3030
_T=TypeVar("_T")
3131

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

158161
deftext(
159162
self,

‎lib/matplotlib/legend.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ from matplotlib.transforms import (
1919
importpathlib
2020
fromcollections.abcimportIterable
2121
fromtypingimportAny,Literal,overload
22-
from .typingimportColorType
22+
from .typingimportColorType,LegendLocType
2323

2424
classDraggableLegend(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
defget_texts(self)->list[Text]: ...
119119
defset_alignment(self,alignment:Literal["center","left","right"])->None: ...
120120
defget_alignment(self)->Literal["center","left","right"]: ...
121-
defset_loc(self,loc:str|tuple[float,float]|int|None= ...)->None: ...
121+
defset_loc(self,loc:LegendLocType|None= ...)->None: ...
122122
defset_title(
123123
self,title:str,prop:FontProperties|str|pathlib.Path|None= ...
124124
)->None: ...

‎lib/matplotlib/typing.py

Lines 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)

[8]ページ先頭

©2009-2025 Movatter.jp