Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add stairstep plotting functionality #1068

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 2 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
10 changes: 10 additions & 0 deletions examples/pylab_examples/stairs_demo.py
Original file line number Diff line number Diff line change
@@ -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()
5 changes: 5 additions & 0 deletions lib/matplotlib/axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
45 changes: 45 additions & 0 deletions lib/matplotlib/stairs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Stairstep plots.
"""

import numpy as np

__all__ = ['stairs']

def stairs(axes, *args, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

"""Draws a stairstep plot
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put the triple quote on its own line.


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)