Skip to content

[MRG+1] plot_date() after axhline() doesn't rescale axes #8205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
2 changes: 2 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a symmetric change to axvline ?

Copy link
Author

@Yasaman-Mah Yasaman-Mah Mar 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made a similar change to axvline. Please see my comment below before approving the changes.

self.autoscale_view(scalex=False, scaley=scaley)
return l

Expand Down Expand Up @@ -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

Expand Down
35 changes: 35 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down