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

Skip to content

Commit 378ac3d

Browse files
committed
feat: add StepPatch baseline array to the demo
1 parent 973072d commit 378ac3d

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

examples/lines_bars_and_markers/stairs_demo.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@
4646
ax.legend()
4747
plt.show()
4848

49+
#############################################################################
50+
# ``baseline`` can take an array to allow for stacked histogram plots
51+
52+
A = np.tile([1,2,3], (3,1)) * np.arange(1, 4).reshape(-1, 1)
53+
A = np.vstack([np.zeros(3), A])
54+
55+
for i in range(3):
56+
plt.stairs(A[i+1], baseline=A[i], fill=True)
57+
4958
#############################################################################
5059
# Comparison of `.pyplot.step` and `.pyplot.stairs`.
5160

lib/matplotlib/patches.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,15 +1031,20 @@ def _update_path(self):
10311031
f"`len(values) = {self._values.size}` and "
10321032
f"`len(edges) = {self._edges.size}`.")
10331033
verts, codes = [], []
1034-
for idx0, idx1 in cbook.contiguous_regions(~np.isnan(self._values)):
1034+
1035+
_nan_vals = ~np.isnan(self._values)
1036+
if self.baseline is not None:
1037+
_nan_vals = _nan_vals & ~np.isnan(self.baseline)
1038+
for idx0, idx1 in cbook.contiguous_regions(_nan_vals):
10351039
x = np.repeat(self._edges[idx0:idx1+1], 2)
10361040
y = np.repeat(self._values[idx0:idx1], 2)
10371041
if np.isscalar(self.baseline): # baseline values
10381042
y = np.hstack((self.baseline, y, self.baseline))
10391043
elif hasattr(self.baseline, '__len__'): # baseline array
10401044
base = np.repeat(self.baseline[idx0:idx1], 2)[::-1]
10411045
x = np.concatenate([x, x[::-1]])
1042-
y = np.concatenate([np.hstack((base[-1], y, base[0], base[0], base, base[-1]))])
1046+
y = np.concatenate([np.hstack((base[-1], y, base[0],
1047+
base[0], base, base[-1]))])
10431048
else: # no baseline
10441049
y = np.hstack((y[0], y, y[-1]))
10451050
if self.orientation == 'vertical':

0 commit comments

Comments
 (0)