From 71dc2d64a233b8702ccdb3c1fa75bff22402e846 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Fri, 25 Sep 2020 23:05:32 +0200 Subject: [PATCH 1/3] Fix stairs() tests --- lib/matplotlib/patches.py | 2 ++ lib/matplotlib/tests/test_axes.py | 42 ++++++++++++++----------------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index 5caa3c7af009..bd89ae64aba3 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -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): diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index e3ae4d2427c9..db1e11a1a67e 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1806,14 +1806,13 @@ 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) @@ -1821,10 +1820,9 @@ def test_stairs(fig_test, fig_ref): 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 = fig_ref.subplots(3, 2).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') @@ -1849,22 +1847,20 @@ def test_stairs(fig_test, fig_ref): 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 = fig_ref.subplots(2, 2).flatten() ref_axes[0].fill_between(bins, np.append(h, h[-1]), step='post') ref_axes[0].set_ylim(0, None) ref_axes[1].fill_betweenx(bins, np.append(h, h[-1]), step='post') @@ -1877,10 +1873,10 @@ def test_stairs_fill(fig_test, fig_ref): ref_axes[3].set_xlim(bs, None) -@check_figures_equal() +@check_figures_equal(extensions=['png']) def test_stairs_update(fig_test, fig_ref): # Test - fig_test, test_ax = plt.subplots() + test_ax = fig_test.add_subplot() h = test_ax.stairs([1, 2, 3]) h.set_values([3, 2, 1]) h.set_edges(np.arange(4)+2) @@ -1893,30 +1889,30 @@ def test_stairs_update(fig_test, fig_ref): assert h.get_baseline() == -2 # # Ref - fig_ref, ref_ax = plt.subplots() + ref_ax = fig_ref.add_subplot() h = ref_ax.stairs([1, 2, 3], baseline=-2) -@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) From 96ebbedc50ddf2c83497909c153342a290b1e988 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Mon, 28 Sep 2020 00:19:09 +0200 Subject: [PATCH 2/3] Adapt Line2D styles in tests so that it matches the PathPatch style --- lib/matplotlib/tests/test_axes.py | 39 +++++++++++++++++-------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index db1e11a1a67e..cdb469465f94 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1822,28 +1822,31 @@ def test_stairs(fig_test, fig_ref): test_axes[5].stairs(y, x, orientation='horizontal') test_axes[5].semilogx() + # defaults of `PathPatch` to be used for all following Line2D + style = {'solid_joinstyle': 'miter', 'solid_capstyle': 'butt'} + ref_axes = fig_ref.subplots(3, 2).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') + 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') - 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[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() @@ -1861,15 +1864,15 @@ def test_stairs_fill(fig_test, fig_ref): # # Ref ref_axes = fig_ref.subplots(2, 2).flatten() - ref_axes[0].fill_between(bins, np.append(h, h[-1]), step='post') + 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) From b2fd5ed4b9b1c7710b21bfe7bdadf5eef37b141c Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Mon, 28 Sep 2020 22:16:56 +0200 Subject: [PATCH 3/3] fix test_stairs_update We have to use fixed view limits because stairs() does autoscale, but updating data does not. Otherwise the test and ref way of generating the plot would end up with different view limits. --- lib/matplotlib/tests/test_axes.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index cdb469465f94..0c962de1a1a1 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1878,9 +1878,12 @@ def test_stairs_fill(fig_test, fig_ref): @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 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) @@ -1891,9 +1894,10 @@ def test_stairs_update(fig_test, fig_ref): h.set_baseline(-2) assert h.get_baseline() == -2 - # # Ref + # Ref ref_ax = fig_ref.add_subplot() h = ref_ax.stairs([1, 2, 3], baseline=-2) + ref_ax.set_ylim(ylim) def test_stairs_invalid_nan():