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

Skip to content

Commit 998c648

Browse files
committed
limit StepPatch to get/set_data()
1 parent b465911 commit 998c648

File tree

4 files changed

+34
-57
lines changed

4 files changed

+34
-57
lines changed

examples/lines_bars_and_markers/stairs_demo.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@
4545

4646
#############################################################################
4747
# *baseline* can take an array to allow for stacked histogram plots
48+
A = [[0, 0, 0],
49+
[1, 2, 3],
50+
[2, 4, 6],
51+
[3, 6, 9]]
4852

49-
A = np.tile([1, 2, 3], (3, 1)) * np.arange(1, 4).reshape(-1, 1)
50-
A = np.vstack([np.zeros(3), A])
51-
52-
for i in range(3):
53+
for i in range(len(A) - 1):
5354
plt.stairs(A[i+1], baseline=A[i], fill=True)
5455

5556
#############################################################################

lib/matplotlib/axes/_axes.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6729,7 +6729,8 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
67296729
def stairs(self, values, edges=None, *,
67306730
orientation='vertical', baseline=0, fill=False, **kwargs):
67316731
"""
6732-
A stepwise constant line or filled plot.
6732+
A stepwise constant function as a line with bounding edges
6733+
or a filled plot.
67336734
67346735
Parameters
67356736
----------
@@ -6745,8 +6746,10 @@ def stairs(self, values, edges=None, *,
67456746
the y-axis, and edges are along the x-axis.
67466747
67476748
baseline : float, array-like or None, default: 0
6748-
Determines bottom value of the bounding edges or when
6749-
``fill=True``, position of lower edge.
6749+
The bottom value of the bounding edges or when
6750+
``fill=True``, position of lower edge. If *fill* is
6751+
True or an array is passed to *baseline*, a closed
6752+
path is drawn.
67506753
67516754
fill : bool, default: False
67526755
Whether the area under the step curve should be filled.

lib/matplotlib/patches.py

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import math
55
from numbers import Number
66
import textwrap
7+
from collections import namedtuple
78

89
import numpy as np
910

@@ -920,7 +921,8 @@ class StepPatch(PathPatch):
920921
"""
921922
A path patch describing a stepwise constant function.
922923
923-
The path is unclosed. It starts and stops at baseline.
924+
By default the path is not closed and starts and stops at
925+
baseline value.
924926
"""
925927

926928
_edge_default = False
@@ -939,12 +941,14 @@ def __init__(self, values, edges, *,
939941
between which the curve takes on vals values.
940942
941943
orientation : {'vertical', 'horizontal'}, default: 'vertical'
942-
The direction of the steps. Vertical means that *values* are along
943-
the y-axis, and edges are along the x-axis.
944+
The direction of the steps. Vertical means that *values* are
945+
along the y-axis, and edges are along the x-axis.
944946
945-
baseline : float, 1D array-like or None, default: 0
946-
Determines bottom value of the bounding edges or when
947-
``fill=True``, position of lower edge.
947+
baseline : float, array-like or None, default: 0
948+
The bottom value of the bounding edges or when
949+
``fill=True``, position of lower edge. If *fill* is
950+
True or an array is passed to *baseline*, a closed
951+
path is drawn.
948952
949953
Other valid keyword arguments are:
950954
@@ -993,12 +997,13 @@ def _update_path(self):
993997
self._path = Path(np.vstack(verts), np.hstack(codes))
994998

995999
def get_data(self):
996-
"""Get `.StepPatch` values, edges and baseline."""
997-
return self._values, self._edges, self._baseline
1000+
"""Get `.StepPatch` values, edges and baseline as namedtuple."""
1001+
StairData = namedtuple('StairData', 'values edges baseline')
1002+
return StairData(self._values, self._edges, self._baseline)
9981003

999-
def set_data(self, values, edges=None, baseline=None):
1004+
def set_data(self, values=None, edges=None, baseline=None):
10001005
"""
1001-
Set `.StepPatch` values and optionally edges and baseline.
1006+
Set `.StepPatch` values, edges and baseline.
10021007
10031008
Parameters
10041009
----------
@@ -1007,6 +1012,8 @@ def set_data(self, values, edges=None, baseline=None):
10071012
edges : 1D array-like, optional
10081013
baseline : float, 1D array-like or None
10091014
"""
1015+
if values is None and edges is None and baseline is None:
1016+
raise ValueError("Must set *values*, *edges* or *baseline*.")
10101017
if values is not None:
10111018
self._values = np.asarray(values)
10121019
if edges is not None:
@@ -1016,40 +1023,6 @@ def set_data(self, values, edges=None, baseline=None):
10161023
self._update_path()
10171024
self.stale = True
10181025

1019-
def set_values(self, values):
1020-
"""
1021-
Set `.StepPatch` values.
1022-
1023-
Parameters
1024-
----------
1025-
values : 1D array-like
1026-
"""
1027-
self.set_data(values, edges=None, baseline=None)
1028-
1029-
def set_edges(self, edges):
1030-
"""
1031-
Set `.StepPatch` edges.
1032-
1033-
Parameters
1034-
----------
1035-
edges : 1D array-like
1036-
"""
1037-
self.set_data(None, edges=edges, baseline=None)
1038-
1039-
def get_baseline(self):
1040-
"""Get `.StepPatch` baseline."""
1041-
return self._baseline
1042-
1043-
def set_baseline(self, baseline):
1044-
"""
1045-
Set `.StepPatch` baseline.
1046-
1047-
Parameters
1048-
----------
1049-
baseline : float, array-like or None, default: 0
1050-
"""
1051-
self.set_data(None, edges=None, baseline=baseline)
1052-
10531026

10541027
class Polygon(Patch):
10551028
"""A general polygon patch."""

lib/matplotlib/tests/test_axes.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,15 +1884,15 @@ def test_stairs_update(fig_test, fig_ref):
18841884
test_ax = fig_test.add_subplot()
18851885
h = test_ax.stairs([1, 2, 3])
18861886
test_ax.set_ylim(ylim)
1887-
h.set_values([3, 2, 1])
1888-
h.set_edges(np.arange(4)+2)
1887+
h.set_data([3, 2, 1])
1888+
h.set_data(edges=np.arange(4)+2)
18891889
h.set_data([1, 2, 1], np.arange(4)/2)
18901890
h.set_data([1, 2, 3])
18911891
h.set_data(None, np.arange(4))
18921892
assert np.allclose(h.get_data()[0], np.arange(1, 4))
18931893
assert np.allclose(h.get_data()[1], np.arange(4))
1894-
h.set_baseline(-2)
1895-
assert h.get_baseline() == -2
1894+
h.set_data(baseline=-2)
1895+
assert h.get_data().baseline == -2
18961896

18971897
# Ref
18981898
ref_ax = fig_ref.add_subplot()
@@ -1913,13 +1913,13 @@ def test_stairs_invalid_mismatch():
19131913
def test_stairs_invalid_update():
19141914
h = plt.stairs([1, 2], [0, 1, 2])
19151915
with pytest.raises(ValueError, match='Nan values in "edges"'):
1916-
h.set_edges([1, np.nan, 2])
1916+
h.set_data(edges=[1, np.nan, 2])
19171917

19181918

19191919
def test_stairs_invalid_update2():
19201920
h = plt.stairs([1, 2], [0, 1, 2])
19211921
with pytest.raises(ValueError, match='Size mismatch'):
1922-
h.set_edges(np.arange(5))
1922+
h.set_data(edges=np.arange(5))
19231923

19241924

19251925
@image_comparison(['test_stairs_options.png'], remove_text=True)

0 commit comments

Comments
 (0)