8000 Merge pull request #18579 from timhoffm/fix-stair-tests · matplotlib/matplotlib@3c1b27f · GitHub
[go: up one dir, main page]

Skip to content

Commit 3c1b27f

Browse files
authored
Merge pull request #18579 from timhoffm/fix-stair-tests
Fix stairs() tests
2 parents 7d0cb13 + b2fd5ed commit 3c1b27f

File tree

2 files changed

+47
-42
lines changed

2 files changed

+47
-42
lines changed

lib/matplotlib/patches.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,8 @@ class StepPatch(PathPatch):
923923
The path is unclosed. It starts and stops at baseline.
924924
"""
925925

926+
_edge_default = False
927+
926928
@docstring.dedent_interpd
927929
def __init__(self, values, edges, *,
928930
orientation='vertical', baseline=0, **kwargs):

lib/matplotlib/tests/test_axes.py

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,82 +1806,84 @@ def test_hist_zorder(histtype, zorder):
18061806
assert patch.get_zorder() == zorder
18071807

18081808

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

1815-
fig_test, test_axes = plt.subplots(3, 2)
1816-
test_axes = test_axes.flatten()
1815+
test_axes = fig_test.subplots(3, 2).flatten()
18171816
test_axes[0].stairs(y, x, baseline=None)
18181817
test_axes[1].stairs(y, x, baseline=None, orientation='horizontal')
18191818
test_axes[2].stairs(y, x)
18201819
test_axes[3].stairs(y, x, orientation='horizontal')
18211820
test_axes[4].stairs(y, x)
18221821
test_axes[4].semilogy()
18231822
test_axes[5].stairs(y, x, orientation='horizontal')
1824-
test_axes[5].semilogy()
1823+
test_axes[5].semilogx()
18251824

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

1831-
ref_axes[2].plot(x, np.append(y, y[-1]), drawstyle='steps-post')
1832-
ref_axes[2].add_line(mlines.Line2D([x[0], x[0]], [0, y[0]]))
1833-
ref_axes[2].add_line(mlines.Line2D([x[-1], x[-1]], [0, y[-1]]))
1828+
ref_axes = fig_ref.subplots(3, 2).flatten()
1829+
ref_axes[0].plot(x, np.append(y, y[-1]), drawstyle='steps-post', **style)
1830+
ref_axes[1].plot(np.append(y[0], y), x, drawstyle='steps-post', **style)
1831+
1832+
ref_axes[2].plot(x, np.append(y, y[-1]), drawstyle='steps-post', **style)
1833+
ref_axes[2].add_line(mlines.Line2D([x[0], x[0]], [0, y[0]], **style))
1834+
ref_axes[2].add_line(mlines.Line2D([x[-1], x[-1]], [0, y[-1]], **style))
18341835
ref_axes[2].set_ylim(0, None)
18351836

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

1841-
ref_axes[4].plot(x, np.append(y, y[-1]), drawstyle='steps-post')
1842-
ref_axes[4].add_line(mlines.Line2D([x[0], x[0]], [0, y[0]]))
1843-
ref_axes[4].add_line(mlines.Line2D([x[-1], x[-1]], [0, y[-1]]))
1842+
ref_axes[4].plot(x, np.append(y, y[-1]), drawstyle='steps-post', **style)
1843+
ref_axes[4].add_line(mlines.Line2D([x[0], x[0]], [0, y[0]], **style))
1844+
ref_axes[4].add_line(mlines.Line2D([x[-1], x[-1]], [0, y[-1]], **style))
18441845
ref_axes[4].semilogy()
18451846

1846-
ref_axes[5].plot(np.append(y[0], y), x, drawstyle='steps-post')
1847-
ref_axes[5].add_line(mlines.Line2D([0, y[0]], [x[0], x[0]]))
1848-
ref_axes[5].add_line(mlines.Line2D([0, y[-1]], [x[-1], x[-1]]))
1847+
ref_axes[5].plot(np.append(y[0], y), x, drawstyle='steps-post', **style)
1848+
ref_axes[5].add_line(mlines.Line2D([0, y[0]], [x[0], x[0]], **style))
1849+
ref_axes[5].add_line(mlines.Line2D([0, y[-1]], [x[-1], x[-1]], **style))
18491850
ref_axes[5].semilogx()
18501851

18511852

1852-
@check_figures_equal()
1853+
@check_figures_equal(extensions=['png'])
18531854
def test_stairs_fill(fig_test, fig_ref):
18541855
h, bins = [1, 2, 3, 4, 2], [0, 1, 2, 3, 4, 5]
18551856
bs = -2
18561857
# Test
1857-
fig_test, test_axes = plt.subplots(2, 2)
1858-
test_axes = test_axes.flatten()
1858+
test_axes = fig_test.subplots(2, 2).flatten()
18591859
test_axes[0].stairs(h, bins, fill=True)
18601860
test_axes[1].stairs(h, bins, orientation='horizontal', fill=True)
18611861
test_axes[2].stairs(h, bins, baseline=bs, fill=True)
18621862
test_axes[3].stairs(h, bins, baseline=bs, orientation='horizontal',
1863-
fill=True)
1863+
fill=True)
18641864

18651865
# # Ref
1866-
fig_ref, ref_axes = plt.subplots(2, 2)
1867-
ref_axes = ref_axes.flatten()
1868-
ref_axes[0].fill_between(bins, np.append(h, h[-1]), step='post')
1866+
ref_axes = fig_ref.subplots(2, 2).flatten()
1867+
ref_axes[0].fill_between(bins, np.append(h, h[-1]), step='post', lw=0)
18691868
ref_axes[0].set_ylim(0, None)
1870-
ref_axes[1].fill_betweenx(bins, np.append(h, h[-1]), step='post')
1869+
ref_axes[1].fill_betweenx(bins, np.append(h, h[-1]), step='post', lw=0)
18711870
ref_axes[1].set_xlim(0, None)
18721871
ref_axes[2].fill_between(bins, np.append(h, h[-1]),
1873-
np.ones(len(h)+1)*bs, step='post')
1872+
np.ones(len(h)+1)*bs, step='post', lw=0)
18741873
ref_axes[2].set_ylim(bs, None)
18751874
ref_axes[3].fill_betweenx(bins, np.append(h, h[-1]),
1876-
np.ones(len(h)+1)*bs, step='post')
1875+
np.ones(len(h)+1)*bs, step='post', lw=0)
18771876
ref_axes[3].set_xlim(bs, None)
18781877

18791878

1880-
@check_figures_equal()
1879+
@check_figures_equal(extensions=['png'])
18811880
def test_stairs_update(fig_test, fig_ref):
1881+
# fixed ylim because stairs() does autoscale, but updating data does not
1882+
ylim = -3, 4
18821883
# Test
1883-
fig_test, test_ax = plt.subplots()
1884+
test_ax = fig_test.add_subplot()
18841885
h = test_ax.stairs([1, 2, 3])
1886+
test_ax.set_ylim(ylim)
18851887
h.set_values([3, 2, 1])
18861888
h.set_edges(np.arange(4)+2)
18871889
h.set_data([1, 2, 1], np.arange(4)/2)
@@ -1892,31 +1894,32 @@ def test_stairs_update(fig_test, fig_ref):
18921894
h.set_baseline(-2)
18931895
assert h.get_baseline() == -2
18941896

1895-
# # Ref
1896-
fig_ref, ref_ax = plt.subplots()
1897+
# Ref
1898+
ref_ax = fig_ref.add_subplot()
18971899
h = ref_ax.stairs([1, 2, 3], baseline=-2)
1900+
ref_ax.set_ylim(ylim)
18981901

18991902

1900-
@pytest.mark.xfail
19011903
def test_stairs_invalid_nan():
1902-
plt.stairs([1, 2], [0, np.nan, 1])
1904+
with pytest.raises(ValueError, match='Nan values in "edges"'):
1905+
plt.stairs([1, 2], [0, np.nan, 1])
19031906

19041907

1905-
@pytest.mark.xfail
19061908
def test_stairs_invalid_mismatch():
1907-
plt.stairs([1, 2], [0, 1])
1909+
with pytest.raises(ValueError, match='Size mismatch'):
1910+
plt.stairs([1, 2], [0, 1])
19081911

19091912

1910-
@pytest.mark.xfail
19111913
def test_stairs_invalid_update():
19121914
h = plt.stairs([1, 2], [0, 1, 2])
1913-
h.set_edges([1, np.nan, 2])
1915+
with pytest.raises(ValueError, match='Nan values in "edges"'):
1916+
h.set_edges([1, np.nan, 2])
19141917

19151918

1916-
@pytest.mark.xfail
19171919
def test_stairs_invalid_update2():
19181920
h = plt.stairs([1, 2], [0, 1, 2])
1919-
h.set_edges(np.arange(5))
1921+
with pytest.raises(ValueError, match='Size mismatch'):
1922+
h.set_edges(np.arange(5))
19201923

19211924

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

0 commit comments

Comments
 (0)
0