@@ -252,27 +252,6 @@ def set_ylabel(self, ylabel, fontdict=None, labelpad=None, **kwargs):
252
252
self .yaxis .labelpad = labelpad
253
253
return self .yaxis .set_label_text (ylabel , fontdict , ** kwargs )
254
254
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
- if legend_handler_map is not None :
266
- handler_map = handler_map .copy ()
267
- handler_map .update (legend_handler_map )
268
-
269
- has_handler = mlegend .Legend .get_legend_handler
270
-
271
- for handle in handles_original :
272
- label = handle .get_label ()
273
- if label != '_nolegend_' and has_handler (handler_map , handle ):
274
- yield handle
275
-
276
255
def get_legend_handles_labels (self , legend_handler_map = None ):
277
256
"""
278
257
Return handles and labels for legend
@@ -283,16 +262,13 @@ def get_legend_handles_labels(self, legend_handler_map=None):
283
262
ax.legend(h, l)
284
263
285
264
"""
286
- handles = []
287
- labels = []
288
- for handle in self ._get_legend_handles (legend_handler_map ):
289
- label = handle .get_label ()
290
- if label and not label .startswith ('_' ):
291
- handles .append (handle )
292
- labels .append (label )
293
265
266
+ # pass through to legend.
267
+ handles , labels = mlegend ._get_legend_handles_labels ([self ],
268
+ legend_handler_map )
294
269
return handles , labels
295
270
271
+ @docstring .dedent_interpd
296
272
def legend (self , * args , ** kwargs ):
297
273
"""
298
274
Places a legend on the axes.
@@ -328,6 +304,7 @@ def legend(self, *args, **kwargs):
328
304
329
305
Parameters
330
306
----------
307
+
331
308
loc : int or string or pair of floats, default: 'upper right'
332
309
The location of the legend. Possible codes are:
333
310
@@ -352,8 +329,7 @@ def legend(self, *args, **kwargs):
352
329
corner of the legend in axes coordinates (in which case
353
330
``bbox_to_anchor`` will be ignored).
354
331
355
- bbox_to_anchor : :class:`matplotlib.transforms.BboxBase` instance \
356
- or tuple of floats
332
+ bbox_to_anchor : `~.BboxBase` or pair of floats
357
333
Specify any arbitrary location for the legend in `bbox_transform`
358
334
coordinates (default Axes coordinates).
359
335
@@ -498,6 +474,11 @@ def legend(self, *args, **kwargs):
498
474
handler. This `handler_map` updates the default handler map
499
475
found at :func:`matplotlib.legend.Legend.get_legend_handler_map`.
500
476
477
+ Returns
478
+ -------
479
+
480
+ :class:`matplotlib.legend.Legend` instance
481
+
501
482
Notes
502
483
-----
503
484
@@ -510,57 +491,12 @@ def legend(self, *args, **kwargs):
510
491
.. plot:: gallery/api/legend.py
511
492
512
493
"""
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 (handles is not None or labels is not None ) and len (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
- if handles and labels :
526
- handles , labels = zip (* zip (handles , labels ))
527
-
528
- elif handles is not None and labels is None :
529
- labels = [handle .get_label () for handle in handles ]
530
- for label , handle in zip (labels [:], handles [:]):
531
- if label .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
- elif labels is not None and handles is None :
539
- # Get as many handles as there are labels.
540
- handles = [handle for handle , label
541
- in zip (self ._get_legend_handles (handlers ), labels )]
542
-
543
- # No arguments - automatically detect labels and handles.
544
- elif len (args ) == 0 :
545
- handles , labels = self .get_legend_handles_labels (handlers )
546
- if not handles :
547
- return None
548
-
549
- # One argument. User defined labels - automatic handle detection.
550
- elif len (args ) == 1 :
551
- labels , = args
552
- # Get as many handles as there are labels.
553
- handles = [handle for handle , label
554
- in zip (self ._get_legend_handles (handlers ), labels )]
555
-
556
- # Two arguments:
557
- # * user defined handles and labels
558
- elif len (args ) == 2 :
559
- handles , labels = args
560
-
561
- else :
562
- raise TypeError ('Invalid arguments to legend.' )
563
-
494
+ handles , labels , extra_args , kwargs = mlegend ._parse_legend_args (
495
+ [self ],
496
+ * args ,
497
+ ** kwargs )
498
+ if len (extra_args ):
499
+ raise TypeError ('legend only accepts two non-keyword arguments' )
564
500
self .legend_ = mlegend .Legend (self , handles , labels , ** kwargs )
565
501
self .legend_ ._remove_method = lambda h : setattr (self , 'legend_' , None )
566
502
return self .legend_
0 commit comments