diff --git a/examples/pylab_examples/stairs_demo.py b/examples/pylab_examples/stairs_demo.py new file mode 100644 index 000000000000..957cdafaeefc --- /dev/null +++ b/examples/pylab_examples/stairs_demo.py @@ -0,0 +1,10 @@ +import numpy as np +from matplotlib import pyplot as plt + +x = np.linspace(-2*np.pi, 2*np.pi, num=40, endpoint=True) +y = np.sin(x) + +fig = plt.figure() +ax = fig.add_subplot(1, 1, 1) +ax.stairs(x, y) +plt.show() diff --git a/lib/matplotlib/axes.py b/lib/matplotlib/axes.py index f631543b7fe2..6c0e3604dc7a 100644 --- a/lib/matplotlib/axes.py +++ b/lib/matplotlib/axes.py @@ -30,6 +30,7 @@ import matplotlib.quiver as mquiver import matplotlib.scale as mscale import matplotlib.stackplot as mstack +import matplotlib.stairs as mstairs import matplotlib.streamplot as mstream import matplotlib.table as mtable import matplotlib.text as mtext @@ -6415,6 +6416,10 @@ def stackplot(self, x, *args, **kwargs): return mstack.stackplot(self, x, *args, **kwargs) stackplot.__doc__ = mstack.stackplot.__doc__ + def stairs(self, *args, **kwargs): + return mstairs.stairs(self, *args, **kwargs) + stairs.__doc__ = mstairs.stairs.__doc__ + def streamplot(self, x, y, u, v, density=1, linewidth=None, color=None, cmap=None, norm=None, arrowsize=1, arrowstyle='-|>', minlength=0.1): diff --git a/lib/matplotlib/stairs.py b/lib/matplotlib/stairs.py new file mode 100644 index 000000000000..83cf1a79815e --- /dev/null +++ b/lib/matplotlib/stairs.py @@ -0,0 +1,45 @@ +""" +Stairstep plots. +""" + +import numpy as np + +__all__ = ['stairs'] + +def stairs(axes, *args, **kwargs): + """Draws a stairstep plot + + Parameters + ---------- + Takes either one or two arguments. Valid calls are: + + ax.stairs(y) # Make a stairstep plot of the values in *y* + ax.stairs(x, y) # Stairstep plot of the values in *y* at points in *x* + + *x*, *y* : 1d arrays. + + Returns + ------- + *lines* : :class:`~matplotlib.collections.LineCollection` + Line collection defining all the steps in the stairstep plot + """ + + if len(args) == 1: + y = np.asarray(args[0]) + x = np.arange(len(y)) + elif len(args) == 2: + x = np.asarray(args[0]) + y = np.asarray(args[1]) + else: + raise ValueError, "stairs takes either 1 or 2 arguments, %d given" % len(args) + + d = 0.5 * np.abs(np.diff(x)) + dm = np.append(d[0], d) + dp = np.append(d, d[-1]) + + xm = x - dm + xp = x + dp + x_all = np.dstack((xm, x, xp)).flatten() + y_all = np.dstack((y, y, y)).flatten() + + return axes.plot(x_all, y_all, **kwargs)