diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 5a0e7d0ce3c7..2902a15fea0b 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -1122,15 +1122,19 @@ def hlines(self, y, xmin, xmax, colors='k', linestyles='solid', if not np.iterable(xmax): xmax = [xmax] - y, xmin, xmax = cbook.delete_masked_points(y, xmin, xmax) - + # Create and combine masked_arrays from input + y, xmin, xmax = cbook._combine_masks(y, xmin, xmax) y = np.ravel(y) - xmin = np.resize(xmin, y.shape) - xmax = np.resize(xmax, y.shape) + xmin = np.ravel(xmin) + xmax = np.ravel(xmax) + + masked_verts = np.ma.empty((len(y), 2, 2)) + masked_verts[:, 0, 0] = xmin + masked_verts[:, 0, 1] = y + masked_verts[:, 1, 0] = xmax + masked_verts[:, 1, 1] = y - verts = [((thisxmin, thisy), (thisxmax, thisy)) - for thisxmin, thisxmax, thisy in zip(xmin, xmax, y)] - lines = mcoll.LineCollection(verts, colors=colors, + lines = mcoll.LineCollection(masked_verts, colors=colors, linestyles=linestyles, label=label) self.add_collection(lines, autolim=False) lines.update(kwargs) @@ -1200,15 +1204,19 @@ def vlines(self, x, ymin, ymax, colors='k', linestyles='solid', if not np.iterable(ymax): ymax = [ymax] - x, ymin, ymax = cbook.delete_masked_points(x, ymin, ymax) - + # Create and combine masked_arrays from input + x, ymin, ymax = cbook._combine_masks(x, ymin, ymax) x = np.ravel(x) - ymin = np.resize(ymin, x.shape) - ymax = np.resize(ymax, x.shape) + ymin = np.ravel(ymin) + ymax = np.ravel(ymax) + + masked_verts = np.ma.empty((len(x), 2, 2)) + masked_verts[:, 0, 0] = x + masked_verts[:, 0, 1] = ymin + masked_verts[:, 1, 0] = x + masked_verts[:, 1, 1] = ymax - verts = [((thisx, thisymin), (thisx, thisymax)) - for thisx, thisymin, thisymax in zip(x, ymin, ymax)] - lines = mcoll.LineCollection(verts, colors=colors, + lines = mcoll.LineCollection(masked_verts, colors=colors, linestyles=linestyles, label=label) self.add_collection(lines, autolim=False) lines.update(kwargs) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 6355b88de91a..5f8a1a514357 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -3992,6 +3992,24 @@ def test_hlines(): ax5.set_ylim(0, 15) +@pytest.mark.parametrize('data', [[1, 2, 3, np.nan, 5], + np.ma.masked_equal([1, 2, 3, 4, 5], 4)]) +@check_figures_equal(extensions=["png"]) +def test_lines_with_colors(fig_test, fig_ref, data): + test_colors = ['red', 'green', 'blue', 'purple', 'orange'] + fig_test.add_subplot(2, 1, 1).vlines(data, 0, 1, + colors=test_colors, linewidth=5) + fig_test.add_subplot(2, 1, 2).hlines(data, 0, 1, + colors=test_colors, linewidth=5) + + expect_xy = [1, 2, 3, 5] + expect_color = ['red', 'green', 'blue', 'orange'] + fig_ref.add_subplot(2, 1, 1).vlines(expect_xy, 0, 1, + colors=expect_color, linewidth=5) + fig_ref.add_subplot(2, 1, 2).hlines(expect_xy, 0, 1, + colors=expect_color, linewidth=5) + + @image_comparison(['step_linestyle', 'step_linestyle'], remove_text=True) def test_step_linestyle(): x = y = np.arange(10)