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

Skip to content

Commit 4acd892

Browse files
committed
BUG : use kwargs if both handles and labels passed
If both handle and labels were passed to Axes.legend they would both be discarded in favor of the auto-generated handles/labels. Also added a warning that if mixed positional/keyword arguments are used (something is going to be ignored).
1 parent 2129a40 commit 4acd892

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

lib/matplotlib/axes/_axes.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,16 @@ def legend(self, *args, **kwargs):
459459
handles = kwargs.pop('handles', None)
460460
labels = kwargs.pop('labels', None)
461461

462-
if handles is not None and labels is None:
462+
if (handles is not None or labels is not None) and len(args):
463+
warnings.warn("You have mixed positional and keyword "
464+
"arguments, some input will be "
465+
"be discarded.")
466+
467+
# if got both handles and labels as kwargs, make same length
468+
if handles and labels:
469+
handles, labels = zip(*zip(handles, labels))
470+
471+
elif handles is not None and labels is None:
463472
labels = [handle.get_label() for handle in handles]
464473
for label, handle in zip(labels[:], handles[:]):
465474
if label.startswith('_'):

0 commit comments

Comments
 (0)