Skip to content

Add new plot style: stackplot #819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
the linear portion relative to the logarithmic portion to be
adjusted. - MGD

2012-04-14 Added new plot style: stackplot. This new feature supports stacked
area plots. - Damon McDougall

2012-04-06 When path clipping changes a LINETO to a MOVETO, it also
changes any CLOSEPOLY command to a LINETO to the initial
point. This fixes a problem with pdf and svg where the
Expand Down
2 changes: 1 addition & 1 deletion boilerplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def %(func)s(%(argspec)s):
return %(ret)s
"""


# Used for colormap functions
CMAP_TEMPLATE = AUTOGEN_MSG + """
def {name}():
Expand Down Expand Up @@ -131,6 +130,7 @@ def boilerplate_gen():
'semilogy',
'specgram',
#'spy',
'stackplot',
'stem',
'step',
'streamplot',
Expand Down
18 changes: 18 additions & 0 deletions examples/pylab_examples/stackplot_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import numpy as np
from matplotlib import pyplot as plt

fnx = lambda : np.random.randint(5, 50, 10)
y = np.row_stack((fnx(), fnx(), fnx()))
x = np.arange(10)

y1, y2, y3 = fnx(), fnx(), fnx()

fig = plt.figure()
ax = fig.add_subplot(111)
ax.stackplot(x, y)
plt.show()

fig = plt.figure()
ax = fig.add_subplot(111)
ax.stackplot(x, y1, y2, y3)
plt.show()
3 changes: 2 additions & 1 deletion lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,8 @@ def tk_window_focus():
'matplotlib.tests.test_text',
'matplotlib.tests.test_tightlayout',
'matplotlib.tests.test_delaunay',
'matplotlib.tests.test_legend'
'matplotlib.tests.test_legend',
'matplotlib.tests.test_stackplot'
]

def test(verbosity=1):
Expand Down
5 changes: 5 additions & 0 deletions lib/matplotlib/axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import matplotlib.spines as mspines
import matplotlib.quiver as mquiver
import matplotlib.scale as mscale
import matplotlib.stackplot as mstack
import matplotlib.streamplot as mstream
import matplotlib.table as mtable
import matplotlib.text as mtext
Expand Down Expand Up @@ -6408,6 +6409,10 @@ def quiver(self, *args, **kw):
return q
quiver.__doc__ = mquiver.Quiver.quiver_doc

def stackplot(self, x, *args, **kwargs):
return mstack.stackplot(self, x, *args, **kwargs)
stackplot.__doc__ = mstack.stackplot.__doc__

def streamplot(self, x, y, u, v, density=1, linewidth=None, color=None,
cmap=None, norm=None, arrowsize=1, arrowstyle='-|>',
minlength=0.1):
Expand Down
19 changes: 19 additions & 0 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,7 @@ def plotting():
until they have been closed; in interactive mode,
show generally has no effect.
specgram a spectrogram plot
stackplot make a stacked plot
stem make a stem plot
subplot make a subplot (numrows, numcols, axesnum)
table add a table to the axes
Expand Down Expand Up @@ -2958,6 +2959,24 @@ def specgram(x, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none,
sci(ret[-1])
return ret

# This function was autogenerated by boilerplate.py. Do not edit as
# changes will be lost
@autogen_docstring(Axes.stackplot)
def stackplot(x, *args, **kwargs):
ax = gca()
# allow callers to override the hold state by passing hold=True|False
washold = ax.ishold()
hold = kwargs.pop('hold', None)
if hold is not None:
ax.hold(hold)
try:
ret = ax.stackplot(x, *args, **kwargs)
draw_if_interactive()
finally:
ax.hold(washold)

return ret

# This function was autogenerated by boilerplate.py. Do not edit as
# changes will be lost
@autogen_docstring(Axes.stem)
Expand Down
60 changes: 60 additions & 0 deletions lib/matplotlib/stackplot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
Stacked area plot for 1D arrays inspired by Douglas Y'barbo's stackoverflow
answer:
http://stackoverflow.com/questions/2225995/how-can-i-create-stacked-line-graph-with-matplotlib

(http://stackoverflow.com/users/66549/doug)

"""
import numpy as np
import matplotlib

__all__ = ['stackplot']


def stackplot(axes, x, *args, **kwargs):
"""Draws a stacked area plot.

Parameters
----------
*x* : 1d array of dimension N
*y* : 2d array of dimension MxN, OR any number 1d arrays each of dimension
1xN. The data is assumed to be unstacked. Each of the following
calls is legal:

stackplot(x, y) # where y is MxN
staclplot(x, y1, y2, y3, y4) # where y1, y2, y3, y4, are all 1xNm

Keyword arguments:
*colors* : A list or tuple of colors. These will be cycled through and
used to colour the stacked areas.
All other keyword arguments are passed to
:func:`~matplotlib.Axes.fill_between`

Returns
-------
*r* : A list of :class:`~matplotlib.collections.PolyCollection`, one for
each element in the stacked area plot.
"""

if len(args) == 1:
y = np.atleast_2d(*args)
elif len(args) > 1:
y = np.row_stack(args)

colors = kwargs.pop('colors', None)
if colors is not None:
axes.set_color_cycle(colors)

# Assume data passed has not been 'stacked', so stack it here.
y_stack = np.cumsum(y, axis=0)

r = []

# Color between x = 0 and the first array.
r.append(axes.fill_between(x, 0, y_stack[0,:], facecolor=axes._get_lines.color_cycle.next(), **kwargs))

# Color between array i-1 and array i
for i in xrange(len(y)-1):
r.append(axes.fill_between(x, y_stack[i,:], y_stack[i+1,:], facecolor=axes._get_lines.color_cycle.next(), **kwargs))
return r
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading