8000 adding labels to stackplot · kkoci/matplotlib@3e73646 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e73646

Browse files
author
Francis Colas
committed
adding labels to stackplot
1 parent e6958c3 commit 3e73646

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/matplotlib/stackplot.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,27 @@ def stackplot(axes, x, *args, **kwargs):
4141
can be found at http://www.leebyron.com/else/streamgraph/.
4242
4343
44+
*labels* : A list or tuple of labels to assign to each data series.
45+
46+
4447
*colors* : A list or tuple of colors. These will be cycled through and
4548
used to colour the stacked areas.
4649
All other keyword arguments are passed to
4750
:func:`~matplotlib.Axes.fill_between`
4851
52+
4953
Returns *r* : A list of
5054
:class:`~matplotlib.collections.PolyCollection`, one for each
5155
element in the stacked area plot.
52-
53-
Note that :class:`~matplotlib.legend.Legend` does not support
54-
:class:`~matplotlib.collections.PolyCollection` objects. To create a
55-
legend on a stackplot, use a proxy artist:
56-
http://matplotlib.org/users/legend_guide.html#using-proxy-artist
5756
"""
5857

5958
if len(args) == 1:
6059
y = np.atleast_2d(*args)
6160
elif len(args) > 1:
6261
y = np.row_stack(args)
6362

63+
labels = iter(kwargs.pop('labels', []))
64+
6465
colors = kwargs.pop('colors', None)
6566
if colors is not None:
6667
axes.set_color_cycle(colors)
@@ -104,12 +105,14 @@ def stackplot(axes, x, *args, **kwargs):
104105
# Color between x = 0 and the first array.
105106
r.append(axes.fill_between(x, first_line, stack[0, :],
106107
facecolor=six.next(axes._get_lines.color_cycle),
108+
label= six.next(labels, None),
107109
**kwargs))
108110

109111
# Color between array i-1 and array i
110112
for i in xrange(len(y) - 1):
111113
color = six.next(axes._get_lines.color_cycle)
112114
r.append(axes.fill_between(x, stack[i, :], stack[i + 1, :],
113115
facecolor= color,
116+
label= six.next(labels, None),
114117
**kwargs))
115118
return r

0 commit comments

Comments
 (0)
0