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

Skip to content

Commit 973072d

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

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-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: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,6 @@ def set_path(self, path):
992992
class StepPatch(PathPatch):
993993
"""An unclosed step path patch."""
994994

995-
@docstring.dedent_interpd
996995
def __init__(self, values, edges, *,
997996
orientation='vertical', baseline=0, **kwargs):
998997
"""
@@ -1007,18 +1006,19 @@ def __init__(self, values, edges, *,
10071006
10081007
orientation : {'vertical', 'horizontal'}, default: 'vertical'
10091008
1010-
baseline : float or None, default: 0
1011-
Determines starting value of the bounding edges or when
1009+
baseline : float, array-like or None, default: 0
1010+
Determines bottom value of the bounding edges or when
10121011
``fill=True``, position of lower edge.
10131012
10141013
Other valid keyword arguments are:
10151014
10161015
%(Patch)s
10171016
"""
1018-
self.baseline = baseline
1017+
self._edge_default = False
10191018
self.orientation = orientation
10201019
self._edges = np.asarray(edges)
10211020
self._values = np.asarray(values)
1021+
self.baseline = baseline
10221022
self._update_path()
10231023
super().__init__(self._path, **kwargs)
10241024

@@ -1033,10 +1033,14 @@ def _update_path(self):
10331033
verts, codes = [], []
10341034
for idx0, idx1 in cbook.contiguous_regions(~np.isnan(self._values)):
10351035
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:
1036+
y = np.repeat(self._values[idx0:idx1], 2)
1037+
if np.isscalar(self.baseline): # baseline values
10381038
y = np.hstack((self.baseline, y, self.baseline))
1039-
else:
1039+
elif hasattr(self.baseline, '__len__'): # baseline array
1040+
base = np.repeat(self.baseline[idx0:idx1], 2)[::-1]
1041+
x = np.concatenate([x, x[::-1]])
1042+
y = np.concatenate([np.hstack((base[-1], y, base[0], base[0], base, base[-1]))])
1043+
else: # no baseline
10401044
y = np.hstack((y[0], y, y[-1]))
10411045
if self.orientation == 'vertical':
10421046
xy = np.column_stack([x, y])
@@ -1045,6 +1049,7 @@ def _update_path(self):
10451049
verts.append(xy)
10461050
codes.append(np.array([Path.MOVETO] + [Path.LINETO]*(len(xy)-1)))
10471051
self._path = Path(np.vstack(verts), np.hstack(codes))
1052+
10481053

10491054
def get_data(self):
10501055
"""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)