8000 API: warn if stairs used in way that is likely not desired · matplotlib/matplotlib@03aa5e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 03aa5e3

Browse files
committed
API: warn if stairs used in way that is likely not desired
Closes #26752
1 parent 69f9ebf commit 03aa5e3

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7194,9 +7194,16 @@ def stairs(self, values, edges=None, *,
71947194
True or an array is passed to *baseline*, a closed
71957195
path is drawn.
71967196
7197+
If None, then drawn as an unclosed Path.
7198+
71977199
fill : bool, default: False
71987200
Whether the area under the step curve should be filled.
71997201
7202+
Passing both ``fill=True` and ``baseline=None`` will likely result in
7203+
undesired filling as the first and last points will be connected
7204+
with a straight line with the fill between and the stairs.
7205+
7206+
72007207
Returns
72017208
-------
72027209
StepPatch : `~matplotlib.patches.StepPatch`
@@ -7234,6 +7241,15 @@ def stairs(self, values, edges=None, *,
72347241
fill=fill,
72357242
**kwargs)
72367243
self.add_patch(patch)
7244+
if baseline is None and fill:
7245+
_api.warn_external(
7246+
f"Both {baseline=} and {fill=} have been passed. "
7247+
"Because baseline in None, the Path used to draw the stairs will "
7248+
"not be closed, thus because fill is True the polygon will be closed "
7249+
"by drawing an (unstroked) edge from the first to last point. It is "
7250+
"very likely that the resulting fill patterns is not the desired "
7251+
"result."
7252+
)
72377253
if baseline is None:
72387254
baseline = 0
72397255
if orientation == 'vertical':

lib/matplotlib/tests/test_axes.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,6 +2342,23 @@ def test_hist_zorder(histtype, zorder):
23422342
assert patch.get_zorder() == zorder
23432343

23442344

2345+
def test_stairs_no_baseline_fill_warns():
2346+
fig, ax = plt.subplots()
2347+
baseline = None
2348+
fill = True
2349+
warn_match = (
2350+
"Because baseline in None, the Path used to draw the stairs will "
2351+
)
2352+
with pytest.warns(UserWarning, match=warn_match):
2353+
ax.stairs(
2354+
[4, 5, 1, 0, 2],
2355+
[1, 2, 3, 4, 5, 6],
2356+
facecolor="blue",
2357+
baseline=baseline,
2358+
fill=fill
2359+
)
2360+
2361+
23452362
@check_figures_equal(extensions=['png'])
23462363
def test_stairs(fig_test, fig_ref):
23472364
import matplotlib.lines as mlines

0 commit comments

Comments
 (0)
0