Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7132ab0

Browse files
committed
format the set_loc() api doc
1 parent 14a88d6 commit 7132ab0

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

lib/matplotlib/legend.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,12 @@ def _update_bbox_to_anchor(self, loc_in_canvas):
330330
_legend_kw_doc_base)
331331
_docstring.interpd.update(_legend_kw_doc=_legend_kw_both_st)
332332

333+
_legend_kw_set_loc_st = (
334+
_loc_doc_base.format(parent='axes/figure',
335+
default=":rc:`legend.loc` for Axes, 'upper right' for Figure",
336+
best=_loc_doc_best, outside=_outside_doc))
337+
_docstring.interpd.update(_legend_kw_set_loc_doc=_legend_kw_set_loc_st)
338+
333339

334340
class Legend(Artist):
335341
"""
@@ -634,19 +640,14 @@ def _set_artist_props(self, a):
634640

635641
a.set_transform(self.get_transform())
636642

637-
def set_loc(self, loc):
643+
@_docstring.dedent_interpd
644+
def set_loc(self, loc=None):
638645
"""
639646
Set the location of the legend.
640647
641648
Parameters
642649
----------
643-
loc : str
644-
{'upper left', 'upper right', 'lower left', 'lower right',
645-
'upper center', 'lower center', 'center left', 'center right'
646-
'center', 'right', 'best'}.
647-
If a figure is using the constrained layout manager,
648-
the string codes of the loc keyword argument can get better
649-
layout behaviour using the prefix 'outside'.
650+
%(_legend_kw_set_loc_doc)s
650651
"""
651652
loc0 = loc
652653
self._loc_used_default = loc is None

lib/matplotlib/legend.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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) -> None: ...
121+
def set_loc(self, loc: str | tuple[float, float] | int | None = ...) -> None: ...
122122
def set_title(
123123
self, title: str, prop: FontProperties | str | pathlib.Path | None = ...
124124
) -> None: ...

lib/matplotlib/tests/test_legend.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,15 +755,26 @@ def test_legend_alignment(alignment):
755755
assert leg.get_alignment() == alignment
756756

757757

758-
@pytest.mark.parametrize('loc', ('center', 'best'))
759-
def test_legend_set_loc(loc):
758+
@pytest.mark.parametrize('loc', ('center', 'best',))
759+
def test_ax_legend_set_loc(loc):
760760
fig, ax = plt.subplots()
761761
ax.plot(range(10), label='test')
762762
leg = ax.legend()
763763
leg.set_loc(loc)
764764
assert leg._get_loc() == mlegend.Legend.codes[loc]
765765

766766

767+
@pytest.mark.parametrize('loc', ('outside right', 'right',))
768+
def test_fig_legend_set_loc(loc):
769+
fig, ax = plt.subplots()
770+
ax.plot(range(10), label='test')
771+
leg = fig.legend()
772+
leg.set_loc(loc)
773+
774+
loc = loc.split()[1] if loc.startswith("outside") else loc
775+
assert leg._get_loc() == mlegend.Legend.codes[loc]
776+
777+
767778
@pytest.mark.parametrize('alignment', ('center', 'left', 'right'))
768779
def test_legend_set_alignment(alignment):
769780
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)