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

Skip to content

Fix inconsistency between set_xlim and set_view_interval #6890

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 9 commits into from
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
35 changes: 33 additions & 2 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2853,9 +2853,26 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
('Attempting to set identical left==right results\n'
'in singular transformations; automatically expanding.\n'
'left=%s, right=%s') % (left, right))

left, right = mtransforms.nonsingular(left, right, increasing=False)
left, right = self.xaxis.limit_range_for_scale(left, right)

self._set_viewLim_intervalx(left, right, emit=emit, auto=auto)
return left, right

def _set_viewLim_intervalx(self, left, right, emit=True, auto=None):
"""
Set the data limits for the xaxis
This should be the only method to change viewLim.intervalx directly.

Keyword arguments:
*emit*: [ *True* | *False* ]
Notify observers of limit change

*auto*: [ *True* | *False* | *None* ]
Turn *x* autoscaling on (*True*), off (*False*; default),
or leave unchanged (*None*)
"""
self.viewLim.intervalx = (left, right)
if auto is not None:
self._autoscaleXon = bool(auto)
Expand All @@ -2871,7 +2888,6 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
other.figure.canvas is not None):
other.figure.canvas.draw_idle()
self.stale = True
return left, right

def get_xscale(self):
return self.xaxis.get_scale()
Expand Down Expand Up @@ -3115,6 +3131,22 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
bottom, top = mtransforms.nonsingular(bottom, top, increasing=False)
bottom, top = self.yaxis.limit_range_for_scale(bottom, top)

self._set_viewLim_intervaly(bottom, top, emit=emit, auto=auto)
return bottom, top

def _set_viewLim_intervaly(self, bottom, top, emit=True, auto=None):
"""
Set the data limits for the yaxis
This should be the only method to change viewLim.intervaly directly.

Keyword arguments:
*emit*: [ *True* | *False* ]
Notify observers of limit change

*auto*: [ *True* | *False* | *None* ]
Turn *x* autoscaling on (*True*), off (*False*; default),
or leave unchanged (*None*)
"""
self.viewLim.intervaly = (bottom, top)
if auto is not None:
self._autoscaleYon = bool(auto)
Expand All @@ -3130,7 +3162,6 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
other.figure.canvas is not None):
other.figure.canvas.draw_idle()
self.stale = True
return bottom, top

def get_yscale(self):
return self.yaxis.get_scale()
Expand Down
23 changes: 12 additions & 11 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1956,15 +1956,16 @@ def set_view_interval(self, vmin, vmax, ignore=False):

"""
if ignore:
self.axes.viewLim.intervalx = vmin, vmax
left, right = vmin, vmax
else:
Vmin, Vmax = self.get_view_interval()
if Vmin < Vmax:
self.axes.viewLim.intervalx = (min(vmin, vmax, Vmin),
max(vmin, vmax, Vmax))
left, right = (min(vmin, vmax, Vmin),
max(vmin, vmax, Vmax))
else:
self.axes.viewLim.intervalx = (max(vmin, vmax, Vmin),
min(vmin, vmax, Vmax))
left, right = (max(vmin, vmax, Vmin),
min(vmin, vmax, Vmax))
self.axes._set_viewLim_intervalx(left, right)

def get_minpos(self):
return self.axes.dataLim.minposx
Expand Down Expand Up @@ -2295,16 +2296,16 @@ def set_view_interval(self, vmin, vmax, ignore=False):

"""
if ignore:
self.axes.viewLim.intervaly = vmin, vmax
bottom, top = vmin, vmax
else:
Vmin, Vmax = self.get_view_interval()
if Vmin < Vmax:
self.axes.viewLim.intervaly = (min(vmin, vmax, Vmin),
max(vmin, vmax, Vmax))
bottom, top = (min(vmin, vmax, Vmin),
max(vmin, vmax, Vmax))
else:
self.axes.viewLim.intervaly = (max(vmin, vmax, Vmin),
min(vmin, vmax, Vmax))
self.stale = True
bottom, top = (max(vmin, vmax, Vmin),
min(vmin, vmax, Vmax))
self.axes._set_viewLim_intervaly(bottom, top)

def get_minpos(self):
return self.axes.dataLim.minposy
Expand Down
13 changes: 13 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ def test_twin_axis_locaters_formatters():
ax3 = ax1.twinx()


@cleanup
def test_twinx_view_interval():
fig, ax = plt.subplots()
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax2 = ax.twinx()
ax3 = ax.twiny()
ax2.set_xticks([1, 2])
ax3.set_yticks([1, 2])
assert ax.get_xlim() == ax2.get_xlim()
assert ax.get_ylim() == ax3.get_ylim()


@cleanup
def test_twinx_cla():
fig, ax = plt.subplots()
Expand Down