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

Skip to content

Commit 08af034

Browse files
committed
feat: StepPatch to take array as baseline
1 parent 33f95db commit 08af034

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6757,8 +6757,8 @@ def stairs(self, values, edges=None, *,
67576757
67586758
orientation : {'vertical', 'horizontal'}, default: 'vertical'
67596759
6760-
baseline : float or None, default: 0
6761-
Determines starting value of the bounding edges or when
6760+
baseline : float, array-like or None, default: 0
6761+
Determines bottom value of the bounding edges or when
67626762
``fill=True``, position of lower edge.
67636763
67646764
fill : bool, default: False
@@ -6787,9 +6787,10 @@ def stairs(self, values, edges=None, *,
67876787
if edges is None:
67886788
edges = np.arange(len(values) + 1)
67896789

6790-
self._process_unit_info(xdata=edges, ydata=values, kwargs=kwargs)
6790+
self._process_unit_info(xdata=edges, ydata=[values, baseline], kwargs=kwargs)
67916791
edges = self.convert_xunits(edges)
67926792
values = self.convert_yunits(values)
6793+
baseline = self.convert_yunits(baseline)
67936794

67946795
patch = mpatches.StepPatch(values,
67956796
edges,
@@ -6801,9 +6802,9 @@ def stairs(self, values, edges=None, *,
68016802
if baseline is None:
68026803
baseline = 0
68036804
if orientation == 'vertical':
6804-
patch.sticky_edges.y.append(baseline)
6805+
patch.sticky_edges.y.append(np.min(baseline))
68056806
else:
6806-
patch.sticky_edges.x.append(baseline)
6807+
patch.sticky_edges.x.append(np.min(baseline))
68076808
self._request_autoscale_view()
68086809
return patch
68096810

lib/matplotlib/patches.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -989,10 +989,10 @@ def set_path(self, path):
989989
self._path = path
990990

991991

992+
992993
class StepPatch(PathPatch):
993994
"""An unclosed step path patch."""
994995

995-
@docstring.dedent_interpd
996996
def __init__(self, values, edges, *,
997997
orientation='vertical', baseline=0, **kwargs):
998998
"""
@@ -1007,18 +1007,19 @@ def __init__(self, values, edges, *,
10071007
10081008
orientation : {'vertical', 'horizontal'}, default: 'vertical'
10091009
1010-
baseline : float or None, default: 0
1011-
Determines starting value of the bounding edges or when
1010+
baseline : float, array-like or None, default: 0
1011+
Determines bottom value of the bounding edges or when
10121012
``fill=True``, position of lower edge.
10131013
10141014
Other valid keyword arguments are:
10151015
10161016
%(Patch)s
10171017
"""
1018-
self.baseline = baseline
1018+
self._edge_default = False
10191019
self.orientation = orientation
10201020
self._edges = np.asarray(edges)
10211021
self._values = np.asarray(values)
1022+
self.baseline = baseline
10221023
self._update_path()
10231024
super().__init__(self._path, **kwargs)
10241025

@@ -1033,10 +1034,14 @@ def _update_path(self):
10331034
verts, codes = [], []
10341035
for idx0, idx1 in cbook.contiguous_regions(~np.isnan(self._values)):
10351036
x = np.repeat(self._edges[idx0:idx1+1], 2)
1036-
y = np.repeat(self._values[idx0:idx1], 2)
1037-
if self.baseline is not None:
1037+
y = np.repeat(self._values[idx0:idx1], 2)
1038+
if np.isscalar(self.baseline): # baseline values
10381039
y = np.hstack((self.baseline, y, self.baseline))
1039-
else:
1040+
elif hasattr(self.baseline, '__len__'): # baseline array
1041+
base = np.repeat(self.baseline[idx0:idx1], 2)[::-1]
1042+
x = np.concatenate([x, x[::-1]])
1043+
y = np.concatenate([np.hstack((base[-1], y, base[0], base[0], base, base[-1]))])
1044+
else: # no baseline
10401045
y = np.hstack((y[0], y, y[-1]))
10411046
if self.orientation == 'vertical':
10421047
xy = np.column_stack([x, y])
@@ -1045,6 +1050,7 @@ def _update_path(self):
10451050
verts.append(xy)
10461051
codes.append(np.array([Path.MOVETO] + [Path.LINETO]*(len(xy)-1)))
10471052
self._path = Path(np.vstack(verts), np.hstack(codes))
1053+
10481054

10491055
def get_data(self):
10501056
"""Get `.StepPatch` values and edges."""

lib/matplotlib/tests/test_axes.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,14 +1928,18 @@ def test_stairs_options():
19281928
fig, ax = plt.subplots()
19291929
ax.stairs(y*3, x, color='green', fill=True, label="A")
19301930
ax.stairs(y, x*3-3, color='red', fill=True,
1931-
orientation='horizontal', label="B")
1931+
orientation='horizontal', label="B")
19321932
ax.stairs(yn, x, color='orange', ls='--', lw=2, label="C")
19331933
ax.stairs(yn/3, x*3-2, ls='--', lw=2, baseline=0.5,
1934-
orientation='horizontal', label="D")
1935-
ax.stairs(y[::-1]*3+12, x, color='red', ls='--', lw=2, baseline=None,
1936-
label="E")
1934+
orientation='horizontal', label="D")
1935+
ax.stairs(y[::-1]*3+13, x-1, color='red', ls='--', lw=2, baseline=None,
1936+
label="E")
1937+
ax.stairs(y[::-1]*3+14, x, baseline=26,
1938+
color='purple', ls='--', lw=2, label="F")
1939+
ax.stairs(yn[::-1]*3+15, x+1, baseline=np.linspace(27, 25, len(y)),
1940+
color='blue', ls='--', lw=2, label="G", fill=True)
19371941
ax.stairs(y[:-1][::-1]*2+11, x[:-1]+0.5, color='black', ls='--', lw=2,
1938-
baseline=12, hatch='//', label="F")
1942+
baseline=12, hatch='//', label="H")
19391943
ax.legend(loc=0)
19401944

19411945

0 commit comments

Comments
 (0)