Skip to content
This repository

Add stairstep plotting functionality #1068

Closed
wants to merge 2 commits into from

5 participants

Damon McDougall Eric Firing Michael Droettboom Phil Elson Benjamin Root
Damon McDougall
Collaborator

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

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

See here for the Matlab interface.

Example output:

example output

added some commits August 10, 2012
Damon McDougall 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
861190a
Damon McDougall Add stairstep plot example c5c4dca
Damon McDougall
Collaborator
Benjamin Root WeatherGod commented on the diff August 10, 2012
lib/matplotlib/stairs.py
... ...
@@ -0,0 +1,45 @@
  1
+"""
  2
+Stairstep plots.
  3
+"""
  4
+
  5
+import numpy as np
  6
+
  7
+__all__ = ['stairs']
  8
+
  9
+def stairs(axes, *args, **kwargs):
1
Benjamin Root Collaborator

To be consistent with the rest of the code-base, I would change "axes" to "ax"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Benjamin Root WeatherGod commented on the diff August 10, 2012
lib/matplotlib/stairs.py
... ...
@@ -0,0 +1,45 @@
  1
+"""
  2
+Stairstep plots.
  3
+"""
  4
+
  5
+import numpy as np
  6
+
  7
+__all__ = ['stairs']
  8
+
  9
+def stairs(axes, *args, **kwargs):
  10
+    """Draws a stairstep plot
1
Benjamin Root Collaborator

Put the triple quote on its own line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Damon McDougall
Collaborator

This should probably be closed since it already exists, as @efiring pointed out. Unless anybody disagrees? It seems moot to duplicate existing functionality.

Michael Droettboom
Owner

Well, perhaps the meta question is: what made it difficult for @dmcdougall to find the existing functionality, and how could that be improved? Maybe we need a patch for that...

Eric Firing
Owner

We do, and Ben started on at least part of the solution, which is putting some organization and editing into the gallery. There is only one step example, and it is burried in the middle; I had to scan the gallery twice to find it, and if I hadn't been pretty sure it was there, I would likely have missed it.

Damon McDougall
Collaborator

I went to the matplotlib homepage, and did a command-f for stairs (because someone on the mailing list was asking how to make stairstep plots).

Also, step is not on the list on the homepage either.

Perhaps re-naming step to stairs (or wrapping it, so people's scripts don't break) so the Matlab and matplotlib naming conventions are more aligned would be useful to avoid future confusion. Or, adding step to the homepage so it can be found more easily.

Michael Droettboom
Owner

Yeah -- that list on the homepage was manually generated many versions ago, so it's incomplete. As part of my reorganization of the front page in #1067 I'm proposing to just get rid of it and funnel people to the pyplot documentation instead. Not sure that's the best solution either, as some people surely find that list useful.

Damon McDougall
Collaborator

I use it every day. I command-f the function I want so I can look at the docs. I always have to check it to remind myself, for example, which element of the extent kwarg list in imshow refers to which bound.

Phil Elson
Collaborator

@mdboom: Maybe a similar table to the one that currently exists could be autogenerated and the content come from the autosummary extension.

Eric Firing
Owner

There is useful discussion here, but as a pull request, this really needs to be closed.

Eric Firing efiring closed this August 13, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Aug 10, 2012
Damon McDougall 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
861190a
Damon McDougall Add stairstep plot example c5c4dca
This page is out of date. Refresh to see the latest.
10  examples/pylab_examples/stairs_demo.py
... ...
@@ -0,0 +1,10 @@
  1
+import numpy as np
  2
+from matplotlib import pyplot as plt
  3
+
  4
+x = np.linspace(-2*np.pi, 2*np.pi, num=40, endpoint=True)
  5
+y = np.sin(x)
  6
+
  7
+fig = plt.figure()
  8
+ax = fig.add_subplot(1, 1, 1)
  9
+ax.stairs(x, y)
  10
+plt.show()
5  lib/matplotlib/axes.py
@@ -30,6 +30,7 @@
30 30
 import matplotlib.quiver as mquiver
31 31
 import matplotlib.scale as mscale
32 32
 import matplotlib.stackplot as mstack
  33
+import matplotlib.stairs as mstairs
33 34
 import matplotlib.streamplot as mstream
34 35
 import matplotlib.table as mtable
35 36
 import matplotlib.text as mtext
@@ -6415,6 +6416,10 @@ def stackplot(self, x, *args, **kwargs):
6415 6416
         return mstack.stackplot(self, x, *args, **kwargs)
6416 6417
     stackplot.__doc__ = mstack.stackplot.__doc__
6417 6418
 
  6419
+    def stairs(self, *args, **kwargs):
  6420
+        return mstairs.stairs(self, *args, **kwargs)
  6421
+    stairs.__doc__ = mstairs.stairs.__doc__
  6422
+
6418 6423
     def streamplot(self, x, y, u, v, density=1, linewidth=None, color=None,
6419 6424
                    cmap=None, norm=None, arrowsize=1, arrowstyle='-|>',
6420 6425
                    minlength=0.1):
45  lib/matplotlib/stairs.py
... ...
@@ -0,0 +1,45 @@
  1
+"""
  2
+Stairstep plots.
  3
+"""
  4
+
  5
+import numpy as np
  6
+
  7
+__all__ = ['stairs']
  8
+
  9
+def stairs(axes, *args, **kwargs):
  10
+    """Draws a stairstep plot
  11
+
  12
+    Parameters
  13
+    ----------
  14
+    Takes either one or two arguments. Valid calls are:
  15
+
  16
+    ax.stairs(y) # Make a stairstep plot of the values in *y*
  17
+    ax.stairs(x, y) # Stairstep plot of the values in *y* at points in *x*
  18
+
  19
+    *x*, *y* : 1d arrays.
  20
+
  21
+    Returns
  22
+    -------
  23
+    *lines* : :class:`~matplotlib.collections.LineCollection`
  24
+              Line collection defining all the steps in the stairstep plot
  25
+    """
  26
+
  27
+    if len(args) == 1:
  28
+        y = np.asarray(args[0])
  29
+        x = np.arange(len(y))
  30
+    elif len(args) == 2:
  31
+        x = np.asarray(args[0])
  32
+        y = np.asarray(args[1])
  33
+    else:
  34
+        raise ValueError, "stairs takes either 1 or 2 arguments, %d given" % len(args)
  35
+
  36
+    d = 0.5 * np.abs(np.diff(x))
  37
+    dm = np.append(d[0], d)
  38
+    dp = np.append(d, d[-1])
  39
+
  40
+    xm = x - dm
  41
+    xp = x + dp
  42
+    x_all = np.dstack((xm, x, xp)).flatten()
  43
+    y_all = np.dstack((y, y, y)).flatten()
  44
+
  45
+    return axes.plot(x_all, y_all, **kwargs)
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.