diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 05f678e2f2f4..c650896bac19 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -919,7 +919,8 @@ def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs): self.autoscale_view(scaley=False) return p - @_preprocess_data(replace_names=['y', 'xmin', 'xmax'], label_namer="y") + @_preprocess_data(replace_names=['y', 'xmin', 'xmax', 'colors'], + label_namer='y') def hlines(self, y, xmin, xmax, colors='k', linestyles='solid', label='', **kwargs): """ @@ -972,17 +973,18 @@ def hlines(self, y, xmin, xmax, colors='k', linestyles='solid', if not iterable(xmax): xmax = [xmax] - y = np.ravel(y) + y, xmin, xmax = cbook.delete_masked_points(y, xmin, xmax) + y = np.ravel(y) xmin = np.resize(xmin, y.shape) xmax = np.resize(xmax, y.shape) verts = [((thisxmin, thisy), (thisxmax, thisy)) for thisxmin, thisxmax, thisy in zip(xmin, xmax, y)] - coll = mcoll.LineCollection(verts, colors=colors, - linestyles=linestyles, label=label) - self.add_collection(coll, autolim=False) - coll.update(kwargs) + lines = mcoll.LineCollection(verts, colors=colors, + linestyles=linestyles, label=label) + self.add_collection(lines, autolim=False) + lines.update(kwargs) if len(y) > 0: minx = min(xmin.min(), xmax.min()) @@ -995,10 +997,10 @@ def hlines(self, y, xmin, xmax, colors='k', linestyles='solid', self.update_datalim(corners) self.autoscale_view() - return coll + return lines - @_preprocess_data(replace_names=["x", "ymin", "ymax", "colors"], - label_namer="x") + @_preprocess_data(replace_names=['x', 'ymin', 'ymax', 'colors'], + label_namer='x') def vlines(self, x, ymin, ymax, colors='k', linestyles='solid', label='', **kwargs): """ @@ -1053,6 +1055,8 @@ def vlines(self, x, ymin, ymax, colors='k', linestyles='solid', if not iterable(ymax): ymax = [ymax] + x, ymin, ymax = cbook.delete_masked_points(x, ymin, ymax) + x = np.ravel(x) ymin = np.resize(ymin, x.shape) ymax = np.resize(ymax, x.shape) @@ -1060,23 +1064,22 @@ def vlines(self, x, ymin, ymax, colors='k', linestyles='solid', verts = [((thisx, thisymin), (thisx, thisymax)) for thisx, thisymin, thisymax in zip(x, ymin, ymax)] #print 'creating line collection' - coll = mcoll.LineCollection(verts, colors=colors, - linestyles=linestyles, label=label) - self.add_collection(coll, autolim=False) - coll.update(kwargs) + lines = mcoll.LineCollection(verts, colors=colors, + linestyles=linestyles, label=label) + self.add_collection(lines, autolim=False) + lines.update(kwargs) if len(x) > 0: - minx = min(x) - maxx = max(x) - - miny = min(min(ymin), min(ymax)) - maxy = max(max(ymin), max(ymax)) + minx = x.min() + maxx = x.max() + miny = min(ymin.min(), ymax.min()) + maxy = max(ymin.max(), ymax.max()) corners = (minx, miny), (maxx, maxy) self.update_datalim(corners) self.autoscale_view() - return coll + return lines @_preprocess_data(replace_names=["positions", "lineoffsets", "linelengths", "linewidths", diff --git a/lib/matplotlib/tests/baseline_images/test_axes/hlines_basic.png b/lib/matplotlib/tests/baseline_images/test_axes/hlines_basic.png new file mode 100644 index 000000000000..6d73964e0d54 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/hlines_basic.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_axes/hlines_masked.png b/lib/matplotlib/tests/baseline_images/test_axes/hlines_masked.png new file mode 100644 index 000000000000..30500a08d538 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/hlines_masked.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_axes/hlines_with_nan.png b/lib/matplotlib/tests/baseline_images/test_axes/hlines_with_nan.png new file mode 100644 index 000000000000..4ab611a2ad6a Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/hlines_with_nan.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_axes/vlines_basic.png b/lib/matplotlib/tests/baseline_images/test_axes/vlines_basic.png new file mode 100644 index 000000000000..9bca1aebcb46 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/vlines_basic.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_axes/vlines_masked.png b/lib/matplotlib/tests/baseline_images/test_axes/vlines_masked.png new file mode 100644 index 000000000000..b328c720be72 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/vlines_masked.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_axes/vlines_with_nan.png b/lib/matplotlib/tests/baseline_images/test_axes/vlines_with_nan.png new file mode 100644 index 000000000000..b4335041bf46 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/vlines_with_nan.png differ diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 22b1289b7f02..74bd9d18ca36 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -2812,6 +2812,90 @@ def test_eb_line_zorder(): ax.set_title("errorbar zorder test") +@image_comparison( + baseline_images=['vlines_basic', 'vlines_with_nan', 'vlines_masked'], + extensions=['png'] +) +def test_vlines(): + # normal + x1 = [2, 3, 4, 5, 7] + y1 = [2, -6, 3, 8, 2] + fig1, ax1 = plt.subplots() + ax1.vlines(x1, 0, y1, colors='g', linewidth=5) + + # GH #7406 + x2 = [2, 3, 4, 5, 6, 7] + y2 = [2, -6, 3, 8, np.nan, 2] + fig2, (ax2, ax3, ax4) = plt.subplots(nrows=3, figsize=(4, 8)) + ax2.vlines(x2, 0, y2, colors='g', linewidth=5) + + x3 = [2, 3, 4, 5, 6, 7] + y3 = [np.nan, 2, -6, 3, 8, 2] + ax3.vlines(x3, 0, y3, colors='r', linewidth=3, linestyle='--') + + x4 = [2, 3, 4, 5, 6, 7] + y4 = [np.nan, 2, -6, 3, 8, np.nan] + ax4.vlines(x4, 0, y4, colors='k', linewidth=2) + + # tweak the x-axis so we can see the lines better + for ax in [ax1, ax2, ax3, ax4]: + ax.set_xlim(0, 10) + + # check that the y-lims are all automatically the same + assert ax1.get_ylim() == ax2.get_ylim() + assert ax1.get_ylim() == ax3.get_ylim() + assert ax1.get_ylim() == ax4.get_ylim() + + fig3, ax5 = plt.subplots() + x5 = np.ma.masked_equal([2, 4, 6, 8, 10, 12], 8) + ymin5 = np.ma.masked_equal([0, 1, -1, 0, 2, 1], 2) + ymax5 = np.ma.masked_equal([13, 14, 15, 16, 17, 18], 18) + ax5.vlines(x5, ymin5, ymax5, colors='k', linewidth=2) + ax5.set_xlim(0, 15) + + +@image_comparison( + baseline_images=['hlines_basic', 'hlines_with_nan', 'hlines_masked'], + extensions=['png'] +) +def test_hlines(): + # normal + y1 = [2, 3, 4, 5, 7] + x1 = [2, -6, 3, 8, 2] + fig1, ax1 = plt.subplots() + ax1.hlines(y1, 0, x1, colors='g', linewidth=5) + + # GH #7406 + y2 = [2, 3, 4, 5, 6, 7] + x2 = [2, -6, 3, 8, np.nan, 2] + fig2, (ax2, ax3, ax4) = plt.subplots(nrows=3, figsize=(4, 8)) + ax2.hlines(y2, 0, x2, colors='g', linewidth=5) + + y3 = [2, 3, 4, 5, 6, 7] + x3 = [np.nan, 2, -6, 3, 8, 2] + ax3.hlines(y3, 0, x3, colors='r', linewidth=3, linestyle='--') + + y4 = [2, 3, 4, 5, 6, 7] + x4 = [np.nan, 2, -6, 3, 8, np.nan] + ax4.hlines(y4, 0, x4, colors='k', linewidth=2) + + # tweak the y-axis so we can see the lines better + for ax in [ax1, ax2, ax3, ax4]: + ax.set_ylim(0, 10) + + # check that the x-lims are all automatically the same + assert ax1.get_xlim() == ax2.get_xlim() + assert ax1.get_xlim() == ax3.get_xlim() + assert ax1.get_xlim() == ax4.get_xlim() + + fig3, ax5 = plt.subplots() + y5 = np.ma.masked_equal([2, 4, 6, 8, 10, 12], 8) + xmin5 = np.ma.masked_equal([0, 1, -1, 0, 2, 1], 2) + xmax5 = np.ma.masked_equal([13, 14, 15, 16, 17, 18], 18) + ax5.hlines(y5, xmin5, xmax5, colors='k', linewidth=2) + ax5.set_ylim(0, 15) + + @image_comparison(baseline_images=['step_linestyle', 'step_linestyle'], remove_text=True) def test_step_linestyle():