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

Skip to content

Commit f4982f1

Browse files
authored
Merge pull request #18543 from timhoffm/stairs
Documentation improvements for stairs()
2 parents ba43765 + 4590f16 commit f4982f1

File tree

5 files changed

+60
-28
lines changed

5 files changed

+60
-28
lines changed

doc/users/next_whats_new/steppatch_and_stairs.rst

Lines changed: 9 additions & 5 deletions
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

Lines changed: 21 additions & 16 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,15 @@ def step(self, x, y, *args, where='pre', data=None, **kwargs):
20092009
formatting options. Most of the concepts and parameters of plot can be
20102010
used here as well.
20112011
2012+
.. note::
2013+
2014+
This method uses a standard plot with a step drawstyle: The *x*
2015+
values are the reference positions and steps extend left/right/both
2016+
directions depending on *where*.
2017+
2018+
For the common case where you know the values and edges of the
2019+
steps, use `~.Axes.stairs` instead.
2020+
20122021
Parameters
20132022
----------
20142023
x : array-like
@@ -6705,24 +6714,27 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
67056714
def stairs(self, values, edges=None, *,
67066715
orientation='vertical', baseline=0, fill=False, **kwargs):
67076716
"""
6708-
A histogram-like line or filled plot.
6717+
A stepwise constant line or filled plot.
67096718
67106719
Parameters
67116720
----------
67126721
values : array-like
6713-
An array of y-values.
6722+
The step heights.
67146723
6715-
edges : array-like, default: ``range(len(vals)+1)``
6716-
A array of x-values, with ``len(edges) == len(vals) + 1``,
6724+
edges : array-like
6725+
The edge positions, with ``len(edges) == len(vals) + 1``,
67176726
between which the curve takes on vals values.
67186727
67196728
orientation : {'vertical', 'horizontal'}, default: 'vertical'
6729+
The direction of the steps. Vertical means that *values* are along
6730+
the y-axis, and edges are along the x-axis.
67206731
67216732
baseline : float or None, default: 0
67226733
Determines starting value of the bounding edges or when
67236734
``fill=True``, position of lower edge.
67246735
67256736
fill : bool, default: False
6737+
Whether the area under the step curve should be filled.
67266738
67276739
Returns
67286740
-------

lib/matplotlib/patches.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,11 @@ def set_path(self, path):
990990

991991

992992
class StepPatch(PathPatch):
993-
"""An unclosed step path patch."""
993+
"""
994+
A path patch describing a stepwise constant function.
995+
996+
The path is unclosed. It starts and stops at baseline.
997+
"""
994998

995999
@docstring.dedent_interpd
9961000
def __init__(self, values, edges, *,
@@ -999,13 +1003,15 @@ def __init__(self, values, edges, *,
9991003
Parameters
10001004
----------
10011005
values : array-like
1002-
An array of y-values.
1006+
The step heights.
10031007
10041008
edges : array-like
1005-
A array of x-value edges, with ``len(edges) == len(vals) + 1``,
1009+
The edge positions, with ``len(edges) == len(vals) + 1``,
10061010
between which the curve takes on vals values.
10071011
10081012
orientation : {'vertical', 'horizontal'}, default: 'vertical'
1013+
The direction of the steps. Vertical means that *values* are along
1014+
the y-axis, and edges are along the x-axis.
10091015
10101016
baseline : float or None, default: 0
10111017
Determines starting value of the bounding edges or when

0 commit comments

Comments
 (0)