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

Skip to content

Commit 85d0e74

Browse files
tacaswelljankatins
authored andcommitted
ENH: extract label from input data in plot
If the input data has a `name` attribute and a label is in explicitly passed in, use the name as the label that will be used in automatic calls to `legend`.
1 parent 346a014 commit 85d0e74

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import matplotlib
1616

1717
from matplotlib import cbook
18-
from matplotlib.cbook import _string_to_bool
18+
from matplotlib.cbook import _string_to_bool, iterable, get_index_y, get_label
1919
from matplotlib import docstring
2020
import matplotlib.colors as mcolors
2121
import matplotlib.lines as mlines
@@ -349,6 +349,9 @@ def _plot_args(self, tup, kwargs):
349349
if v is not None:
350350
kw[k] = v
351351

352+
if 'label' not in kwargs or kwargs['label'] is None:
353+
kwargs['label'] = get_label(tup[-1])
354+
352355
if len(tup) == 2:
353356
x = np.atleast_1d(tup[0])
354357
y = np.atleast_1d(tup[-1])

lib/matplotlib/cbook.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2515,6 +2515,13 @@ def get_index_y(y):
25152515
y = np.atleast_1d(y)
25162516
return np.arange(y.shape[0], dtype=float), y
25172517

2518+
2519+
def get_label(y):
2520+
try:
2521+
return y.name
2522+
except AttributeError:
2523+
return None
2524+
25182525
# Numpy > 1.6.x deprecates putmask in favor of the new copyto.
25192526
# So long as we support versions 1.6.x and less, we need the
25202527
# following local version of putmask. We choose to make a

0 commit comments

Comments
 (0)