10000 Fixes #7742 - axhline rescale issue · sughandj/matplotlib@7cf097d · GitHub
[go: up one dir, main page]

Skip to content

Commit 7cf097d

Browse files
committed
Fixes matplotlib#7742 - axhline rescale issue
Modified axhline() function to save state of ignoring limits and restore after plotting the line
1 parent 31af0e7 commit 7cf097d

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,8 +724,12 @@ def axhline(self, y=0, xmin=0, xmax=1, **kwargs):
724724

725725
trans = self.get_yaxis_transform(which='grid')
726726
l = mlines.Line2D([xmin, xmax], [y, y], transform=trans, **kwargs)
727+
728+
# Save state for ignoring existing data limits and set it back later
729+
ignore = self.ignore_existing_data_limits
727730
self.add_line(l)
728731
self.autoscale_view(scalex=False, scaley=scaley)
732+
self.ignore_existing_data_limits = ignore
729733
return l
730734

731735
@docstring.dedent_interpd

lib/matplotlib/tests/test_axes.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4972,3 +4972,32 @@ def test_bar_single_height():
49724972
ax.bar(range(4), 1)
49734973
# Check that a horizontal chart with one width works
49744974
ax.bar(0, 1, bottom=range(4), width=1, orientation='horizontal')
4975+
4976+
4977+
def test_axhline_datetime_limits():
4978+
# Plot horizontal line with other lines in
4979+
# different order and check if they have the same limits
4980+
date1 = datetime.datetime(2016, 1, 1, 0, 0, 0)
4981+
date2 = datetime.datetime(2016, 1, 30, 0, 0, 0)
4982+
date3 = datetime.datetime(2016, 1, 10, 0, 0, 0)
4983+
4984+
dates1 = [date1, date2]
4985+
values1 = [1, 2]
4986+
4987+
dates2 = [date1, date3]
4988+
values2 = [0, 5]
4989+
4990+
fig, (ax1, ax2) = plt.subplots(2, 1)
4991+
4992+
ax1.axhline(1.5)
4993+
ax1.plot(dates1, values1)
4994+
4995+
ax2.plot(dates1, values1)
4996+
ax2.axhline(1.5)
4997+
4998+
assert ax1.get_xlim() == ax2.get_xlim()
4999+
5000+
ax1.plot(dates2, values2)
5001+
ax2.plot(dates2, values2)
5002+
5003+
assert ax1.get_xlim() == ax2.get_xlim()

0 commit comments

Comments
 (0)
0