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

Commitc1fe98d

Browse files
committed
MNT: re-arrange where check is done
1 parent31cb0c8 commitc1fe98d

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

‎lib/matplotlib/figure.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,16 @@ def legend(self, *args, **kwargs):
10871087
----------------
10881088
%(_legend_kw_doc)s
10891089
1090+
If a figure is using the constrained layout manager, the string codes
1091+
of the *loc* keyword argument can get better layout behaviour using the
1092+
prefix 'outside'. There is ambiguity at the corners, so 'outside
1093+
upper right' will make space for the legend above the rest of the
1094+
axes in the layout, and 'outside right upper' will make space on the
1095+
right side of the layout. In addition to the values of *loc*
1096+
listed above, we have 'outside right upper', 'outside right lower',
1097+
'outside left upper', and 'outside left lower'. See
1098+
:doc:`/tutorials/intermediate/legend_guide` for more details.
1099+
10901100
See Also
10911101
--------
10921102
.Axes.legend
@@ -1113,32 +1123,9 @@ def legend(self, *args, **kwargs):
11131123
# extra_args = extra_args[1:]
11141124
pass
11151125
transform=kwargs.pop('bbox_transform',self.transSubfigure)
1116-
outside=None
1117-
if'loc'inkwargs:
1118-
# need to protect this in case loc was passed in
1119-
# positionally...
1120-
loc=kwargs.pop('loc',None)
1121-
# handle outside legends:
1122-
ifisinstance(loc,str):
1123-
ifloc.split()[0]=='outside':
1124-
# strip outside:
1125-
loc=loc.split('outside ')[1]
1126-
# strip "center" at the beginning
1127-
outside=loc.replace('center ','')
1128-
# strip first
1129-
outside=outside.split()[0]
1130-
locs=loc.split()
1131-
iflen(locs)>1andlocs[0]in ('right','left'):
1132-
# locs doesn't accept "left upper", etc, so swap
1133-
iflocs[0]!='center':
1134-
locs=locs[::-1]
1135-
loc=locs[0]+' '+locs[1]
1136-
1137-
kwargs['loc']=loc
11381126
# explicitly set the bbox transform if the user hasn't.
11391127
l=mlegend.Legend(self,handles,labels,*extra_args,
11401128
bbox_transform=transform,**kwargs)
1141-
l._outside=outside
11421129
self.legends.append(l)
11431130
l._remove_method=self.legends.remove
11441131
self.stale=True

‎lib/matplotlib/legend.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,39 @@ def val_or_rc(val, rc_name):
465465
)
466466
self.parent=parent
467467

468+
loc0=loc
468469
self._loc_used_default=locisNone
469470
iflocisNone:
470471
loc=mpl.rcParams["legend.loc"]
471472
ifnotself.isaxesandlocin [0,'best']:
472473
loc='upper right'
474+
475+
# handle outside legends:
476+
self._outside=None
473477
ifisinstance(loc,str):
478+
ifloc.split()[0]=='outside':
479+
# strip outside:
480+
loc=loc.split('outside ')[1]
481+
# strip "center" at the beginning
482+
self._outside=loc.replace('center ','')
483+
# strip first
484+
self._outside=self._outside.split()[0]
485+
locs=loc.split()
486+
iflen(locs)>1andlocs[0]in ('right','left'):
487+
# locs doesn't accept "left upper", etc, so swap
488+
iflocs[0]!='center':
489+
locs=locs[::-1]
490+
loc=locs[0]+' '+locs[1]
491+
# check that loc is in acceptable strings
474492
loc=_api.check_getitem(self.codes,loc=loc)
493+
494+
ifself.isaxesandself._outside:
495+
# warn if user has done "outside upper right", but don't
496+
# error because at this point we just drop the "outside".
497+
raiseUserWarning(
498+
f"'outside' option for loc='{loc0}' keyword argument only "
499+
"works for figure legends")
500+
475501
ifnotself.isaxesandloc==0:
476502
raiseValueError(
477503
"Automatic legend placement (loc='best') not implemented for "

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp