diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index dc7b82386216..f137d15e226f 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -725,6 +725,7 @@ def axhline(self, y=0, xmin=0, xmax=1, **kwargs): trans = self.get_yaxis_transform(which='grid') l = mlines.Line2D([xmin, xmax], [y, y], transform=trans, **kwargs) self.add_line(l) + self.ignore_existing_data_limits = True self.autoscale_view(scalex=False, scaley=scaley) return l @@ -791,6 +792,7 @@ def axvline(self, x=0, ymin=0, ymax=1, **kwargs): trans = self.get_xaxis_transform(which='grid') l = mlines.Line2D([x, x], [ymin, ymax], transform=trans, **kwargs) self.add_line(l) + self.ignore_existing_data_limits = True self.autoscale_view(scalex=scalex, scaley=False) return l diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index dcebf02af1be..0115cfcd151e 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -4974,6 +4974,41 @@ def test_bar_single_height(): ax.bar(0, 1, bottom=range(4), width=1, orientation='horizontal') +def test_datetime_axhline_same_axes(): + # This test was suggested by Paul Hobson (@phobson) regarding issue 7742 + # Check when ploting datetime data and horizontal line + # order of plotting doesn't matter. + # axhline() should not change x axis limits. + from datetime import datetime + fig, axs = plt.subplots(2, 1) + xvalues = [datetime(2016, 1, 1, 0, 0, 0), datetime(2016, 1, 2, 0, 0, 0)] + yvalues = [1, 2] + + axs[0].plot(xvalues, yvalues) + axs[0].axhline(1.5) + + axs[1].axhline(1.5) + axs[1].plot(xvalues, yvalues) + + assert (axs[0].get_xlim() == axs[1].get_xlim()) + + +def test_datetime_axvline_same_axes(): + # This is similar to test above + from datetime import datetime + fig, axs = plt.subplots(2, 1) + xvalues = [1, 2] + yvalues = [datetime(2016, 1, 1, 0, 0, 0), datetime(2016, 1, 2, 0, 0, 0)] + + axs[0].plot(xvalues, yvalues) + axs[0].axvline(1.5) + + axs[1].axvline(1.5) + axs[1].plot(xvalues, yvalues) + + assert (axs[0].get_ylim() == axs[1].get_ylim()) + + def test_invalid_axis_limits(): plt.plot([0, 1], [0, 1]) with pytest.raises(ValueError):