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

Skip to content

Commit c2a6c1b

Browse files
legend-loc-compass-notation
1 parent 74640bb commit c2a6c1b

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

lib/matplotlib/legend.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def _update_bbox_to_anchor(self, loc_in_canvas):
154154
'center' 10
155155
=============== =============
156156
157+
157158
bbox_to_anchor : `.BboxBase`, 2-tuple, or 4-tuple of floats
158159
Box that is used to position the legend in conjunction with *loc*.
159160
Defaults to `axes.bbox` (if called as a method to `.Axes.legend`) or
@@ -335,7 +336,9 @@ class Legend(Artist):
335336
'upper center': 9,
336337
'center': 10,
337338
}
338-
339+
compasscodes = {'nw': 2, 'n': 9, 'ne': 1, 'w': 6, 'c': 10, 'e': 7,
340+
'sw': 3, 's': 8, 'se': 4}
341+
allcodes = {**codes, **compasscodes}
339342
zorder = 5
340343

341344
def __str__(self):
@@ -505,23 +508,23 @@ def __init__(self, parent, handles, labels,
505508
if not self.isaxes and loc in [0, 'best']:
506509
loc = 'upper right'
507510
if isinstance(loc, str):
508-
if loc not in self.codes:
511+
if loc.lower() not in self.allcodes:
509512
if self.isaxes:
510513
cbook.warn_deprecated(
511514
"3.1", message="Unrecognized location {!r}. Falling "
512515
"back on 'best'; valid locations are\n\t{}\n"
513516
"This will raise an exception %(removal)s."
514-
.format(loc, '\n\t'.join(self.codes)))
517+
.format(loc, '\n\t'.join(self.allcodes)))
515518
loc = 0
516519
else:
517520
cbook.warn_deprecated(
518521
"3.1", message="Unrecognized location {!r}. Falling "
519522
"back on 'upper right'; valid locations are\n\t{}\n'"
520523
"This will raise an exception %(removal)s."
521-
.format(loc, '\n\t'.join(self.codes)))
524+
.format(loc, '\n\t'.join(self.allcodes)))
522525
loc = 1
523526
else:
524-
loc = self.codes[loc]
527+
loc = self.allcodes[loc.lower()]
525528
if not self.isaxes and loc == 0:
526529
cbook.warn_deprecated(
527530
"3.1", message="Automatic legend placement (loc='best') not "

lib/matplotlib/rcsetup.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -601,16 +601,16 @@ def validate_markevery(s):
601601
validate_legend_loc = ValidateInStrings(
602602
'legend_loc',
603603
['best',
604-
'upper right',
605-
'upper left',
606-
'lower left',
607-
'lower right',
604+
'upper right', 'ne',
605+
'upper left', 'nw',
606+
'lower left', 'sw',
607+
'lower right', 'se',
608608
'right',
609-
'center left',
610-
'center right',
611-
'lower center',
612-
'upper center',
613-
'center'], ignorecase=True)
609+
'center left', 'w',
610+
'center right', 'e',
611+
'lower center', 's',
612+
'upper center', 'n',
613+
'center', 'c'], ignorecase=True)
614614

615615

616616
def validate_svg_fonttype(s):

lib/matplotlib/tests/test_legend.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,3 +571,32 @@ def test_no_warn_big_data_when_loc_specified():
571571
l = ax.legend('best')
572572
fig.canvas.draw()
573573
assert len(records) == 0
574+
575+
576+
def test_legend_loc_compass_codes():
577+
locs = ['NW', 'N', 'NE', 'W', 'C', 'E', 'SW', 'S', 'SE']
578+
codes = [2, 9, 1, 6, 10, 7, 3, 8, 4]
579+
580+
for loc in locs:
581+
plt.rcParams["legend.loc"] = loc
582+
assert plt.rcParams["legend.loc"] == loc.lower()
583+
584+
fig1, axes1 = plt.subplots(3, 3)
585+
fig2, axes2 = plt.subplots(3, 3)
586+
587+
locs = ['NW', 'N', 'NE', 'W', 'C', 'E', 'SW', 'S', 'SE']
588+
codes = [2, 9, 1, 6, 10, 7, 3, 8, 4]
589+
590+
for ax, loc in zip(axes1.flat, locs):
591+
ax.plot([1, 2], label=loc)
592+
ax.legend(loc=loc)
593+
594+
for ax, loc in zip(axes2.flat, codes):
595+
ax.plot([1, 2], label=loc)
596+
ax.legend(loc=loc)
597+
598+
for ax1, ax2 in zip(axes1.flat, axes2.flat):
599+
leg1 = ax1.get_legend()
600+
leg2 = ax2.get_legend()
601+
assert leg1._get_loc() == leg2._get_loc()
602+

0 commit comments

Comments
 (0)