Skip to content

Commit 4590f16

Browse files
committed
Relate step() to stairs()
1 parent 0522695 commit 4590f16

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

doc/users/next_whats_new/steppatch_and_stairs.rst

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
New StepPatch artist and a stairs method
22
----------------------------------------
3-
New `~.matplotlib.patches.StepPatch` artist and `.pyplot.stairs` method.
4-
For both the artist and the function, the x-like edges input is one
3+
`.pyplot.stairs` and the underlying artist `~.matplotlib.patches.StepPatch`
4+
provide a cleaner interface for plotting stepwise constant functions for the
5+
common case that you know the step edges. This superseeds many use cases of
6+
`.pyplot.step`, for instance when plotting the output of `numpy.histogram`.
7+
8+
For both the artist and the function, the x-like edges input is one element
59
longer than the y-like values input
610

711
.. plot::
@@ -10,12 +14,12 @@ longer than the y-like values input
1014
import matplotlib.pyplot as plt
1115

1216
np.random.seed(0)
13-
h, bins = np.histogram(np.random.normal(5, 2, 5000),
14-
bins=np.linspace(0,10,20))
17+
h, edges = np.histogram(np.random.normal(5, 2, 5000),
18+
bins=np.linspace(0,10,20))
1519

1620
fig, ax = plt.subplots(constrained_layout=True)
1721

18-
ax.stairs(h, bins)
22+
ax.stairs(h, edges)
1923

2024
plt.show()
2125

examples/lines_bars_and_markers/stairs_demo.py

+21-16
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,24 @@
33
Stairs Demo
44
===========
55
6-
This example demonstrates the use of `~.matplotlib.pyplot.stairs`
7-
for histogram and histogram-like data visualization and an associated
8-
underlying `.StepPatch` artist, which is a specialized version of
9-
`.PathPatch` specified by its bins and edges.
6+
This example demonstrates the use of `~.matplotlib.pyplot.stairs` for stepwise
7+
constant functions. A common use case is histogram and histogram-like data
8+
visualization.
109
11-
The primary difference to `~.matplotlib.pyplot.step` is that ``stairs``
12-
x-like edges input is one longer than its y-like values input.
1310
"""
1411

1512
import numpy as np
1613
import matplotlib.pyplot as plt
1714
from matplotlib.patches import StepPatch
1815

1916
np.random.seed(0)
20-
h, bins = np.histogram(np.random.normal(5, 3, 5000),
21-
bins=np.linspace(0, 10, 20))
17+
h, edges = np.histogram(np.random.normal(5, 3, 5000),
18+
bins=np.linspace(0, 10, 20))
2219

2320
fig, axs = plt.subplots(3, 1, figsize=(7, 15))
24-
axs[0].stairs(h, bins, label='Simple histogram')
25-
axs[0].stairs(h, bins+5, baseline=50, label='Modified baseline')
26-
axs[0].stairs(h, bins+10, baseline=None, label='No edges')
21+
axs[0].stairs(h, edges, label='Simple histogram')
22+
axs[0].stairs(h, edges + 5, baseline=50, label='Modified baseline')
23+
axs[0].stairs(h, edges + 10, baseline=None, label='No edges')
2724
axs[0].set_title("Step Histograms")
2825

2926
axs[1].stairs(np.arange(1, 6, 1), fill=True,
@@ -47,22 +44,30 @@
4744
plt.show()
4845

4946
#############################################################################
50-
# Comparison of `.pyplot.step` and `.pyplot.stairs`.
47+
# Comparison of `.pyplot.step` and `.pyplot.stairs`
48+
# -------------------------------------------------
49+
#
50+
# `.pyplot.step` defines the positions of the steps as single values. The steps
51+
# extend left/right/both ways from these reference values depending on the
52+
# parameter *where*. The number of *x* and *y* values is the same.
53+
#
54+
# In contrast, `.pyplot.stairs` defines the positions of the steps via their
55+
# bounds *edges*, which is one element longer than the step values.
5156

5257
bins = np.arange(14)
53-
centers = bins[:-1] + np.diff(bins)/2
58+
centers = bins[:-1] + np.diff(bins) / 2
5459
y = np.sin(centers / 2)
5560

56-
plt.step(bins[:-1], y, where='post', label='Step(where="post")')
61+
plt.step(bins[:-1], y, where='post', label='step(where="post")')
5762
plt.plot(bins[:-1], y, 'o--', color='grey', alpha=0.3)
5863

59-
plt.stairs(y - 1, bins, baseline=None, label='Stairs')
64+
plt.stairs(y - 1, bins, baseline=None, label='stairs()')
6065
plt.plot(centers, y - 1, 'o--', color='grey', alpha=0.3)
6166
plt.plot(np.repeat(bins, 2), np.hstack([y[0], np.repeat(y, 2), y[-1]]) - 1,
6267
'o', color='red', alpha=0.2)
6368

6469
plt.legend()
65-
plt.title('plt.step vs plt.stairs')
70+
plt.title('step() vs. stairs()')
6671
plt.show()
6772

6873
#############################################################################

examples/lines_bars_and_markers/step_demo.py

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
curves. In particular, it illustrates the effect of the parameter *where*
88
on the step position.
99
10+
.. note::
11+
12+
For the common case that you know the edge positions, use `.pyplot.stairs`
13+
instead.
14+
1015
The circular markers created with `.pyplot.plot` show the actual data
1116
positions so that it's easier to see the effect of *where*.
1217

lib/matplotlib/axes/_axes.py

+9
Original file line numberDiff line numberDiff line change
@@ -2036,6 +2036,15 @@ def step(self, x, y, *args, where='pre', data=None, **kwargs):
20362036
formatting options. Most of the concepts and parameters of plot can be
20372037
used here as well.
20382038
2039+
.. note::
2040+
2041+
This method uses a standard plot with a step drawstyle: The *x*
2042+
values are the reference positions and steps extend left/right/both
2043+
directions depending on *where*.
2044+
2045+
For the common case where you know the values and edges of the
2046+
steps, use `~.Axes.stairs` instead.
2047+
20392048
Parameters
20402049
----------
20412050
x : array-like

0 commit comments

Comments
 (0)