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

Commit6516c1c

Browse files
committed
Allow kwarg handles and labels figure.legend
Allow kwarg handles and labels figure.legendSmall refactor of check for labels so it is inside legend.Legendattempt to refactor legend; not passing testssmall typoMoved kwarg documentation out of child functions and into legend.Fixed documentation passthroughs so they work properlyFixed documentation passthroughs so they work properlyRemerge master, small changesFixed testsRemove repeated labels if same linestyleRemove repeated labels if same linestyleRemoved ability of third positional argument for axes.legendDeprecate third psotionsal argument for fig.legendAllow parasite axes to call legend(): parasite_simple.pyFixed docFixed docAdded twinx test for legendmerge fixmerge fixFixed legend handling of handles-only input; added error messageFixed legend handling of handles-only input for py27small doc change legend
1 parente10c523 commit6516c1c

File tree

5 files changed

+742
-248
lines changed

5 files changed

+742
-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
@@ -497,6 +474,11 @@ def legend(self, *args, **kwargs):
497474
handler. This `handler_map` updates the default handler map
498475
found at :func:`matplotlib.legend.Legend.get_legend_handler_map`.
499476
477+
Returns
478+
-------
479+
480+
:class:`matplotlib.legend.Legend` instance
481+
500482
Notes
501483
-----
502484
@@ -509,57 +491,12 @@ def legend(self, *args, **kwargs):
509491
.. plot:: gallery/api/legend.py
510492
511493
"""
512-
handlers=kwargs.get('handler_map', {})or {}
513-
514-
# Support handles and labels being passed as keywords.
515-
handles=kwargs.pop('handles',None)
516-
labels=kwargs.pop('labels',None)
517-
518-
if (handlesisnotNoneorlabelsisnotNone)andlen(args):
519-
warnings.warn("You have mixed positional and keyword "
520-
"arguments, some input will be "
521-
"discarded.")
522-
523-
# if got both handles and labels as kwargs, make same length
524-
ifhandlesandlabels:
525-
handles,labels=zip(*zip(handles,labels))
526-
527-
elifhandlesisnotNoneandlabelsisNone:
528-
labels= [handle.get_label()forhandleinhandles]
529-
forlabel,handleinzip(labels[:],handles[:]):
530-
iflabel.startswith('_'):
531-
warnings.warn('The handle {!r} has a label of {!r} which '
532-
'cannot be automatically added to the '
533-
'legend.'.format(handle,label))
534-
labels.remove(label)
535-
handles.remove(handle)
536-
537-
eliflabelsisnotNoneandhandlesisNone:
538-
# Get as many handles as there are labels.
539-
handles= [handleforhandle,label
540-
inzip(self._get_legend_handles(handlers),labels)]
541-
542-
# No arguments - automatically detect labels and handles.
543-
eliflen(args)==0:
544-
handles,labels=self.get_legend_handles_labels(handlers)
545-
ifnothandles:
546-
returnNone
547-
548-
# One argument. User defined labels - automatic handle detection.
549-
eliflen(args)==1:
550-
labels,=args
551-
# Get as many handles as there are labels.
552-
handles= [handleforhandle,label
553-
inzip(self._get_legend_handles(handlers),labels)]
554-
555-
# Two arguments:
556-
# * user defined handles and labels
557-
eliflen(args)==2:
558-
handles,labels=args
559-
560-
else:
561-
raiseTypeError('Invalid arguments to legend.')
562-
494+
handles,labels,extra_args,kwargs=mlegend._parse_legend_args(
495+
[self],
496+
*args,
497+
**kwargs)
498+
iflen(extra_args):
499+
raiseTypeError('legend only accepts two non-keyword arguments')
563500
self.legend_=mlegend.Legend(self,handles,labels,**kwargs)
564501
self.legend_._remove_method=lambdah:setattr(self,'legend_',None)
565502
returnself.legend_

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp