Skip to content

Fix stairs() tests #18579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/matplotlib/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,8 @@ class StepPatch(PathPatch):
The path is unclosed. It starts and stops at baseline.
"""

_edge_default = False

@docstring.dedent_interpd
def __init__(self, values, edges, *,
orientation='vertical', baseline=0, **kwargs):
Expand Down
87 changes: 45 additions & 42 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1806,82 +1806,84 @@ def test_hist_zorder(histtype, zorder):
assert patch.get_zorder() == zorder


@check_figures_equal()
@check_figures_equal(extensions=['png'])
def test_stairs(fig_test, fig_ref):
import matplotlib.lines as mlines
y = np.array([6, 14, 32, 37, 48, 32, 21, 4]) # hist
x = np.array([1., 2., 3., 4., 5., 6., 7., 8., 9.]) # bins

fig_test, test_axes = plt.subplots(3, 2)
test_axes = test_axes.flatten()
test_axes = fig_test.subplots(3, 2).flatten()
test_axes[0].stairs(y, x, baseline=None)
test_axes[1].stairs(y, x, baseline=None, orientation='horizontal')
test_axes[2].stairs(y, x)
test_axes[3].stairs(y, x, orientation='horizontal')
test_axes[4].stairs(y, x)
test_axes[4].semilogy()
test_axes[5].stairs(y, x, orientation='horizontal')
test_axes[5].semilogy()
test_axes[5].semilogx()

fig_ref, ref_axes = plt.subplots(3, 2)
ref_axes = ref_axes.flatten()
ref_axes[0].plot(x, np.append(y, y[-1]), drawstyle='steps-post')
ref_axes[1].plot(np.append(y[0], y), x, drawstyle='steps-post')
# defaults of `PathPatch` to be used for all following Line2D
style = {'solid_joinstyle': 'miter', 'solid_capstyle': 'butt'}

ref_axes[2].plot(x, np.append(y, y[-1]), drawstyle='steps-post')
ref_axes[2].add_line(mlines.Line2D([x[0], x[0]], [0, y[0]]))
ref_axes[2].add_line(mlines.Line2D([x[-1], x[-1]], [0, y[-1]]))
ref_axes = fig_ref.subplots(3, 2).flatten()
ref_axes[0].plot(x, np.append(y, y[-1]), drawstyle='steps-post', **style)
ref_axes[1].plot(np.append(y[0], y), x, drawstyle='steps-post', **style)

ref_axes[2].plot(x, np.append(y, y[-1]), drawstyle='steps-post', **style)
ref_axes[2].add_line(mlines.Line2D([x[0], x[0]], [0, y[0]], **style))
ref_axes[2].add_line(mlines.Line2D([x[-1], x[-1]], [0, y[-1]], **style))
ref_axes[2].set_ylim(0, None)

ref_axes[3].plot(np.append(y[0], y), x, drawstyle='steps-post')
ref_axes[3].add_line(mlines.Line2D([0, y[0]], [x[0], x[0]]))
ref_axes[3].add_line(mlines.Line2D([0, y[-1]], [x[-1], x[-1]]))
ref_axes[3].plot(np.append(y[0], y), x, drawstyle='steps-post', **style)
ref_axes[3].add_line(mlines.Line2D([0, y[0]], [x[0], x[0]], **style))
ref_axes[3].add_line(mlines.Line2D([0, y[-1]], [x[-1], x[-1]], **style))
ref_axes[3].set_xlim(0, None)

ref_axes[4].plot(x, np.append(y, y[-1]), drawstyle='steps-post')
ref_axes[4].add_line(mlines.Line2D([x[0], x[0]], [0, y[0]]))
ref_axes[4].add_line(mlines.Line2D([x[-1], x[-1]], [0, y[-1]]))
ref_axes[4].plot(x, np.append(y, y[-1]), drawstyle='steps-post', **style)
ref_axes[4].add_line(mlines.Line2D([x[0], x[0]], [0, y[0]], **style))
ref_axes[4].add_line(mlines.Line2D([x[-1], x[-1]], [0, y[-1]], **style))
ref_axes[4].semilogy()

ref_axes[5].plot(np.append(y[0], y), x, drawstyle='steps-post')
ref_axes[5].add_line(mlines.Line2D([0, y[0]], [x[0], x[0]]))
ref_axes[5].add_line(mlines.Line2D([0, y[-1]], [x[-1], x[-1]]))
ref_axes[5].plot(np.append(y[0], y), x, drawstyle='steps-post', **style)
ref_axes[5].add_line(mlines.Line2D([0, y[0]], [x[0], x[0]], **style))
ref_axes[5].add_line(mlines.Line2D([0, y[-1]], [x[-1], x[-1]], **style))
ref_axes[5].semilogx()


@check_figures_equal()
@check_figures_equal(extensions=['png'])
def test_stairs_fill(fig_test, fig_ref):
h, bins = [1, 2, 3, 4, 2], [0, 1, 2, 3, 4, 5]
bs = -2
# Test
fig_test, test_axes = plt.subplots(2, 2)
test_axes = test_axes.flatten()
test_axes = fig_test.subplots(2, 2).flatten()
test_axes[0].stairs(h, bins, fill=True)
test_axes[1].stairs(h, bins, orientation='horizontal', fill=True)
test_axes[2].stairs(h, bins, baseline=bs, fill=True)
test_axes[3].stairs(h, bins, baseline=bs, orientation='horizontal',
fill=True)
fill=True)

# # Ref
fig_ref, ref_axes = plt.subplots(2, 2)
ref_axes = ref_axes.flatten()
ref_axes[0].fill_between(bins, np.append(h, h[-1]), step='post')
ref_axes = fig_ref.subplots(2, 2).flatten()
ref_axes[0].fill_between(bins, np.append(h, h[-1]), step='post', lw=0)
ref_axes[0].set_ylim(0, None)
ref_axes[1].fill_betweenx(bins, np.append(h, h[-1]), step='post')
ref_axes[1].fill_betweenx(bins, np.append(h, h[-1]), step='post', lw=0)
ref_axes[1].set_xlim(0, None)
ref_axes[2].fill_between(bins, np.append(h, h[-1]),
np.ones(len(h)+1)*bs, step='post')
np.ones(len(h)+1)*bs, step='post', lw=0)
ref_axes[2].set_ylim(bs, None)
ref_axes[3].fill_betweenx(bins, np.append(h, h[-1]),
np.ones(len(h)+1)*bs, step='post')
np.ones(len(h)+1)*bs, step='post', lw=0)
ref_axes[3].set_xlim(bs, None)


@check_figures_equal()
@check_figures_equal(extensions=['png'])
def test_stairs_update(fig_test, fig_ref):
# fixed ylim because stairs() does autoscale, but updating data does not
ylim = -3, 4
# Test
fig_test, test_ax = plt.subplots()
test_ax = fig_test.add_subplot()
h = test_ax.stairs([1, 2, 3])
test_ax.set_ylim(ylim)
h.set_values([3, 2, 1])
h.set_edges(np.arange(4)+2)
h.set_data([1, 2, 1], np.arange(4)/2)
Expand All @@ -1892,31 +1894,32 @@ def test_stairs_update(fig_test, fig_ref):
h.set_baseline(-2)
assert h.get_baseline() == -2

# # Ref
fig_ref, ref_ax = plt.subplots()
# Ref
ref_ax = fig_ref.add_subplot()
h = ref_ax.stairs([1, 2, 3], baseline=-2)
ref_ax.set_ylim(ylim)


@pytest.mark.xfail
def test_stairs_invalid_nan():
plt.stairs([1, 2], [0, np.nan, 1])
with pytest.raises(ValueError, match='Nan values in "edges"'):
plt.stairs([1, 2], [0, np.nan, 1])


@pytest.mark.xfail
def test_stairs_invalid_mismatch():
plt.stairs([1, 2], [0, 1])
with pytest.raises(ValueError, match='Size mismatch'):
plt.stairs([1, 2], [0, 1])


@pytest.mark.xfail
def test_stairs_invalid_update():
h = plt.stairs([1, 2], [0, 1, 2])
h.set_edges([1, np.nan, 2])
with pytest.raises(ValueError, match='Nan values in "edges"'):
h.set_edges([1, np.nan, 2])


@pytest.mark.xfail
def test_stairs_invalid_update2():
h = plt.stairs([1, 2], [0, 1, 2])
h.set_edges(np.arange(5))
with pytest.raises(ValueError, match='Size mismatch'):
h.set_edges(np.arange(5))


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