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

Skip to content

Commit 93d288e

Browse files
committed
add public set_loc method for
1 parent dbc906a commit 93d288e

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

lib/matplotlib/legend.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,19 @@ def _set_artist_props(self, a):
686686

687687
a.set_transform(self.get_transform())
688688

689+
def set_loc(self, loc):
690+
"""
691+
Set the location of the legend.
692+
693+
Parameters
694+
----------
695+
alignment : {'upper left', 'upper right', 'lower left', 'lower right',
696+
'upper center', 'lower center', 'center left', 'center right'
697+
'center', 'right', 'best'}.
698+
"""
699+
_loc_code = self.codes.get(loc, 'best')
700+
self._set_loc(_loc_code)
701+
689702
def _set_loc(self, loc):
690703
# find_offset function will be provided to _legend_box and
691704
# _legend_box will draw itself at the location of the return

lib/matplotlib/tests/test_legend.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,15 @@ 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):
760+
fig, ax = plt.subplots()
761+
ax.plot(range(10), label='test')
762+
leg = ax.legend()
763+
leg.set_loc(loc)
764+
assert leg._get_loc() == mlegend.Legend.codes[loc]
765+
766+
758767
@pytest.mark.parametrize('alignment', ('center', 'left', 'right'))
759768
def test_legend_set_alignment(alignment):
760769
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)