Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fixes #7742 - axhline rescale issue #8210

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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,8 +724,12 @@ 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)

# Save state for ignoring existing data limits and set it back later
ignore = self.ignore_existing_data_limits
self.add_line(l)
self.autoscale_view(scalex=False, scaley=scaley)
self.ignore_existing_data_limits = ignore
return l

@docstring.dedent_interpd
Expand Down
29 changes: 29 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4972,3 +4972,32 @@ def test_bar_single_height():
ax.bar(range(4), 1)
# Check that a horizontal chart with one width works
ax.bar(0, 1, bottom=range(4), width=1, orientation='horizontal')


def test_axhline_datetime_limits():
# Plot horizontal line with other lines in
# different order and check if they have the same limits
date1 = datetime.datetime(2016, 1, 1, 0, 0, 0)
date2 = datetime.datetime(2016, 1, 30, 0, 0, 0)
date3 = datetime.datetime(2016, 1, 10, 0, 0, 0)

dates1 = [date1, date2]
values1 = [1, 2]

dates2 = [date1, date3]
values2 = [0, 5]

fig, (ax1, ax2) = plt.subplots(2, 1)

ax1.axhline(1.5)
ax1.plot(dates1, values1)

ax2.plot(dates1, values1)
ax2.axhline(1.5)

assert ax1.get_xlim() == ax2.get_xlim()

ax1.plot(dates2, values2)
ax2.plot(dates2, values2)

assert ax1.get_xlim() == ax2.get_xlim()