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

Skip to content

Commit e5af734

Browse files
committed
fix: stairs/steppatch cleanup
1 parent f2499b9 commit e5af734

File tree

6 files changed

+61
-29
lines changed

6 files changed

+61
-29
lines changed

doc/api/axes_api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ Binned
137137
Axes.hexbin
138138
Axes.hist
139139
Axes.hist2d
140+
Axes.stairs
140141

141142
Contours
142143
--------

doc/users/next_whats_new/steppatch_and_stairs.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
New `~.matplotlib.patches.StepPatch` artist and a `.pyplot.stairs` method
22
-------------------------------------------------------------------------
3-
These take inputs of asymmetric lengths with y-like values and
4-
x-like edges, between which the values lie.
3+
For both the artist and the function, the x-like edges input is one
4+
longer than the y-like values input
55

66
.. plot::
77

examples/lines_bars_and_markers/stairs_demo.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
"""
2-
=============
3-
Histline Demo
4-
=============
2+
===========
3+
Stairs Demo
4+
===========
55
66
This example demonstrates the use of `~.matplotlib.pyplot.stairs`
77
for histogram and histogram-like data visualization and an associated
8-
underlying `.StepPatch` artist, which is
9-
a contrained version of `.PathPatch` specified by its bins and edges.
8+
underlying `.StepPatch` artist, which is a specialized version of
9+
`.PathPatch` specified by its bins and edges.
10+
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.
1013
"""
1114

1215
import numpy as np
@@ -19,12 +22,12 @@
1922

2023
fig, axs = plt.subplots(3, 1, figsize=(7, 15))
2124
axs[0].stairs(h, bins, label='Simple histogram')
22-
axs[0].stairs(h, bins+5, baseline=50, label='--//-- w/ modified baseline')
23-
axs[0].stairs(h, bins+10, baseline=None, label='--//-- w/ no edges')
25+
axs[0].stairs(h, bins+5, baseline=50, label='Modified baseline')
26+
axs[0].stairs(h, bins+10, baseline=None, label='No edges')
2427
axs[0].set_title("Step Histograms")
2528

2629
axs[1].stairs(np.arange(1, 6, 1), fill=True,
27-
label='Filled histogram\nw/ automatatic edges')
30+
label='Filled histogram\nw/ automatic edges')
2831
axs[1].stairs(np.arange(1, 6, 1)*0.3, np.arange(2, 8, 1),
2932
orientation='horizontal', hatch='//',
3033
label='Hatched histogram\nw/ horizontal orientation')
@@ -43,6 +46,24 @@
4346
ax.legend()
4447
plt.show()
4548

49+
#############################################################################
50+
# Comparison of `.pyplot.step` and `.pyplot.stairs`.
51+
52+
bins = np.arange(14)
53+
centers = bins[:-1] + np.diff(bins)/2
54+
y = np.sin(centers / 2)
55+
56+
plt.step(bins[:-1], y, where='post', label='Step(where="post")')
57+
plt.plot(bins[:-1], y, 'o--', color='grey', alpha=0.3)
58+
59+
plt.stairs(y - 1, bins, baseline=None, label='Stairs')
60+
plt.plot(centers, y - 1, 'o--', color='grey', alpha=0.3)
61+
plt.plot(np.repeat(bins, 2), np.hstack([y[0], np.repeat(y, 2), y[-1]]) - 1,
62+
'o', color='red', alpha=0.2)
63+
64+
plt.legend()
65+
plt.title('plt.step vs plt.stairs')
66+
plt.show()
4667

4768
#############################################################################
4869
#

lib/matplotlib/axes/_axes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6918,6 +6918,10 @@ def stairs(self, values, edges=None, *,
69186918
if edges is None:
69196919
edges = np.arange(len(values) + 1)
69206920

6921+
self._process_unit_info(xdata=edges, ydata=values, kwargs=kwargs)
6922+
edges = self.convert_xunits(edges)
6923+
values = self.convert_yunits(values)
6924+
69216925
patch = mpatches.StepPatch(values,
69226926
edges,
69236927
baseline=baseline,

lib/matplotlib/patches.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,11 +1019,10 @@ def __init__(self, values, edges, *,
10191019
self.orientation = orientation
10201020
self._edges = np.asarray(edges)
10211021
self._values = np.asarray(values)
1022-
verts, codes = self._update_data()
1023-
path = Path(verts, codes)
1024-
super().__init__(path, **kwargs)
1022+
self._update_path()
1023+
super().__init__(self._path, **kwargs)
10251024

1026-
def _update_data(self):
1025+
def _update_path(self):
10271026
if np.isnan(np.sum(self._edges)):
10281027
raise ValueError('Nan values in "edges" are disallowed')
10291028
if self._edges.size - 1 != self._values.size:
@@ -1046,18 +1045,24 @@ def _update_data(self):
10461045
xy = np.column_stack([y, x])
10471046
verts.append(xy)
10481047
codes.append(np.array([Path.MOVETO] + [Path.LINETO]*(len(xy)-1)))
1049-
return np.vstack(verts), np.hstack(codes)
1048+
self._path = Path(np.vstack(verts), np.hstack(codes))
10501049

1051-
def set_edges(self, edges):
1052-
self._edges = np.asarray(edges)
1053-
verts, codes = self._update_data()
1054-
self.set_path(Path(verts, codes))
1055-
self.stale = True
1050+
def set_data(self, values, edges=None):
1051+
"""
1052+
Set the values and optionally edges of the
1053+
StepPatch artist.
10561054
1057-
def set_values(self, values):
1058-
self._values = np.asarray(values)
1059-
verts, codes = self._update_data()
1060-
self.set_path(Path(verts, codes))
1055+
Parameters
1056+
----------
1057+
values : 1D array-like or None
1058+
Will not update values, if passing None
1059+
edges : 1D array-like, optional
1060+
"""
1061+
if values is not None:
1062+
self._values = np.asarray(values)
1063+
if edges is not None:
1064+
self._edges = np.asarray(edges)
1065+
self._update_path()
10611066
self.stale = True
10621067

10631068

lib/matplotlib/tests/test_axes.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,13 +1819,14 @@ def test_stairs_fill(fig_test, fig_ref):
18191819
def test_stairs_update(fig_test, fig_ref):
18201820
# Test
18211821
fig_test, test_ax = plt.subplots()
1822-
h = test_ax.stairs([1, 2])
1823-
h.set_values([2, 1])
1824-
h.set_edges([0, 1, 1.5])
1825-
1822+
h = test_ax.stairs([1, 2, 3])
1823+
h.set_values([3, 2, 1])
1824+
h.set_data([1, 2, 1], np.arange(4)/2)
1825+
h.set_data([1, 2, 3])
1826+
h.set_data(None, np.arange(4))
18261827
# # Ref
18271828
fig_ref, ref_ax = plt.subplots()
1828-
h = ref_ax.stairs([2, 1], [0, 1, 1.5])
1829+
h = ref_ax.stairs([1, 2, 3])
18291830

18301831

18311832
@pytest.mark.xfail

0 commit comments

Comments
 (0)