8000 Merge pull request #28033 from tacaswell/mnt/stairs_tweaks · matplotlib/matplotlib@ea8e95e · GitHub
[go: up one dir, main page]

Skip to content

Commit ea8e95e

Browse files
authored
Merge pull request #28033 from tacaswell/mnt/stairs_tweaks
API: warn if stairs used in way that is likely not desired
2 parents 5e0aa54 + 79e947b commit ea8e95e

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7205,9 +7205,16 @@ def stairs(self, values, edges=None, *,
72057205
True or an array is passed to *baseline*, a closed
72067206
path is drawn.
72077207
7208+
If None, then drawn as an unclosed Path.
7209+
72087210
fill : bool, default: False
72097211
Whether the area under the step curve should be filled.
72107212
7213+
Passing both ``fill=True` and ``baseline=None`` will likely result in
7214+
undesired filling: the first and last points will be connected
7215+
with a straight line and the fill will be between this line and the stairs.
7216+
7217+
72117218
Returns
72127219
-------
72137220
StepPatch : `~matplotlib.patches.StepPatch`
@@ -7245,6 +7252,16 @@ def stairs(self, values, edges=None, *,
72457252
fill=fill,
72467253
**kwargs)
72477254
self.add_patch(patch)
7255+
if baseline is None and fill:
7256+
_api.warn_external(
7257+
f"Both {baseline=} and {fill=} have been passed. "
7258+
"baseline=None is only intended for unfilled stair plots. "
7259+
"Because baseline is None, the Path used to draw the stairs will "
7260+
"not be closed, thus because fill is True the polygon will be closed "
7261+
"by drawing an (unstroked) edge from the first to last point. It is "
7262+
"very likely that the resulting fill patterns is not the desired "
7263+
"result."
7264+
)
72487265
if baseline is None:
72497266
baseline = 0
72507267
if orientation == 'vertical':

lib/matplotlib/tests/test_axes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,6 +2342,18 @@ 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+
with pytest.warns(UserWarning, match="baseline=None and fill=True"):
2348+
ax.stairs(
2349+
[4, 5, 1, 0, 2],
2350+
[1, 2, 3, 4, 5, 6],
2351+
facecolor="blue",
2352+
baseline=None,
2353+
fill=True
2354+
)
2355+
2356+
23452357
@check_figures_equal(extensions=['png'])
23462358
def test_stairs(fig_test, fig_ref):
23472359
import matplotlib.lines as mlines

0 commit comments

Comments
 (0)
0