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

Commit4f594f4

Browse files
tacaswellMeeseeksDev[bot]
authored and
MeeseeksDev[bot]
committed
Backport PR#9324: [MRG] Allow kwarg handles and labels figure.legend and make doc for kwargs the same
1 parent9527ba5 commit4f594f4

File tree

5 files changed

+753
-248
lines changed

5 files changed

+753
-248
lines changed

‎lib/matplotlib/axes/_axes.py

Lines changed: 16 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -252,27 +252,6 @@ def set_ylabel(self, ylabel, fontdict=None, labelpad=None, **kwargs):
252252
self.yaxis.labelpad=labelpad
253253
returnself.yaxis.set_label_text(ylabel,fontdict,**kwargs)
254254

255-
def_get_legend_handles(self,legend_handler_map=None):
256-
"""
257-
Return a generator of artists that can be used as handles in
258-
a legend.
259-
260-
"""
261-
handles_original= (self.lines+self.patches+
262-
self.collections+self.containers)
263-
handler_map=mlegend.Legend.get_default_handler_map()
264-
265-
iflegend_handler_mapisnotNone:
266-
handler_map=handler_map.copy()
267-
handler_map.update(legend_handler_map)
268-
269-
has_handler=mlegend.Legend.get_legend_handler
270-
271-
forhandleinhandles_original:
272-
label=handle.get_label()
273-
iflabel!='_nolegend_'andhas_handler(handler_map,handle):
274-
yieldhandle
275-
276255
defget_legend_handles_labels(self,legend_handler_map=None):
277256
"""
278257
Return handles and labels for legend
@@ -283,16 +262,13 @@ def get_legend_handles_labels(self, legend_handler_map=None):
283262
ax.legend(h, l)
284263
285264
"""
286-
handles= []
287-
labels= []
288-
forhandleinself._get_legend_handles(legend_handler_map):
289-
label=handle.get_label()
290-
iflabelandnotlabel.startswith('_'):
291-
handles.append(handle)
292-
labels.append(label)
293265

266+
# pass through to legend.
267+
handles,labels=mlegend._get_legend_handles_labels([self],
268+
legend_handler_map)
294269
returnhandles,labels
295270

271+
@docstring.dedent_interpd
296272
deflegend(self,*args,**kwargs):
297273
"""
298274
Places a legend on the axes.
@@ -328,6 +304,7 @@ def legend(self, *args, **kwargs):
328304
329305
Parameters
330306
----------
307+
331308
loc : int or string or pair of floats, default: 'upper right'
332309
The location of the legend. Possible codes are:
333310
@@ -498,6 +475,11 @@ def legend(self, *args, **kwargs):
498475
handler. This `handler_map` updates the default handler map
499476
found at :func:`matplotlib.legend.Legend.get_legend_handler_map`.
500477
478+
Returns
479+
-------
480+
481+
:class:`matplotlib.legend.Legend` instance
482+
501483
Notes
502484
-----
503485
@@ -510,57 +492,12 @@ def legend(self, *args, **kwargs):
510492
.. plot:: gallery/api/legend.py
511493
512494
"""
513-
handlers=kwargs.get('handler_map', {})or {}
514-
515-
# Support handles and labels being passed as keywords.
516-
handles=kwargs.pop('handles',None)
517-
labels=kwargs.pop('labels',None)
518-
519-
if (handlesisnotNoneorlabelsisnotNone)andlen(args):
520-
warnings.warn("You have mixed positional and keyword "
521-
"arguments, some input will be "
522-
"discarded.")
523-
524-
# if got both handles and labels as kwargs, make same length
525-
ifhandlesandlabels:
526-
handles,labels=zip(*zip(handles,labels))
527-
528-
elifhandlesisnotNoneandlabelsisNone:
529-
labels= [handle.get_label()forhandleinhandles]
530-
forlabel,handleinzip(labels[:],handles[:]):
531-
iflabel.startswith('_'):
532-
warnings.warn('The handle {!r} has a label of {!r} which '
533-
'cannot be automatically added to the '
534-
'legend.'.format(handle,label))
535-
labels.remove(label)
536-
handles.remove(handle)
537-
538-
eliflabelsisnotNoneandhandlesisNone:
539-
# Get as many handles as there are labels.
540-
handles= [handleforhandle,label
541-
inzip(self._get_legend_handles(handlers),labels)]
542-
543-
# No arguments - automatically detect labels and handles.
544-
eliflen(args)==0:
545-
handles,labels=self.get_legend_handles_labels(handlers)
546-
ifnothandles:
547-
returnNone
548-
549-
# One argument. User defined labels - automatic handle detection.
550-
eliflen(args)==1:
551-
labels,=args
552-
# Get as many handles as there are labels.
553-
handles= [handleforhandle,label
554-
inzip(self._get_legend_handles(handlers),labels)]
555-
556-
# Two arguments:
557-
# * user defined handles and labels
558-
eliflen(args)==2:
559-
handles,labels=args
560-
561-
else:
562-
raiseTypeError('Invalid arguments to legend.')
563-
495+
handles,labels,extra_args,kwargs=mlegend._parse_legend_args(
496+
[self],
497+
*args,
498+
**kwargs)
499+
iflen(extra_args):
500+
raiseTypeError('legend only accepts two non-keyword arguments')
564501
self.legend_=mlegend.Legend(self,handles,labels,**kwargs)
565502
self.legend_._remove_method=lambdah:setattr(self,'legend_',None)
566503
returnself.legend_

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp