From 861190a195297d60622f617306d2002b61f0d424 Mon Sep 17 00:00:00 2001 From: Damon McDougall Date: Fri, 10 Aug 2012 18:48:56 +0100 Subject: [PATCH 1/2] Add stairstep plotting functionality This feature replicates Matlab's `stairs' function to produce stairstep type plots. These plots are useful for plots in signal processing for looking at discretely sampled data. Valid function calls are stairs(y) # Make a stairstep plot of the values in y stairs(x, y) # Stairstep plot of the values in y at points in x --- lib/matplotlib/axes.py | 5 +++++ lib/matplotlib/stairs.py | 45 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 lib/matplotlib/stairs.py 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) From c5c4dcaac3ca71dda819c6ced4fa026847a6cc22 Mon Sep 17 00:00:00 2001 From: Damon McDougall Date: Fri, 10 Aug 2012 19:06:52 +0100 Subject: [PATCH 2/2] Add stairstep plot example --- examples/pylab_examples/stairs_demo.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 examples/pylab_examples/stairs_demo.py 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() pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy