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

Skip to content

Backport PR #14598 on branch v3.1.x (Fix inversion of shared axes.) #14619

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

Merged
Merged
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
19 changes: 14 additions & 5 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,11 +1004,10 @@ def set_inverted(self, inverted):
the top for the y-axis; the "inverse" direction is increasing to the
left for the x-axis and to the bottom for the y-axis.
"""
a, b = self.get_view_interval()
if inverted:
self.set_view_interval(max(a, b), min(a, b), ignore=True)
else:
self.set_view_interval(min(a, b), max(a, b), ignore=True)
# Currently, must be implemented in subclasses using set_xlim/set_ylim
# rather than generically using set_view_interval, so that shared
# axes get updated as well.
raise NotImplementedError('Derived must override')

def set_default_intervals(self):
"""
Expand Down Expand Up @@ -2156,6 +2155,11 @@ def get_ticks_position(self):
def get_minpos(self):
return self.axes.dataLim.minposx

def set_inverted(self, inverted):
# docstring inherited
a, b = self.get_view_interval()
self.axes.set_xlim(sorted((a, b), reverse=inverted), auto=None)

def set_default_intervals(self):
# docstring inherited
xmin, xmax = 0., 1.
Expand Down Expand Up @@ -2458,6 +2462,11 @@ def get_ticks_position(self):
def get_minpos(self):
return self.axes.dataLim.minposy

def set_inverted(self, inverted):
# docstring inherited
a, b = self.get_view_interval()
self.axes.set_ylim(sorted((a, b), reverse=inverted), auto=None)

def set_default_intervals(self):
# docstring inherited
ymin, ymax = 0., 1.
Expand Down
12 changes: 7 additions & 5 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,17 +244,19 @@ def test_inverted_cla():
ax.cla()
ax.imshow(img)
plt.autoscale()
assert not(ax.xaxis_inverted())
assert not ax.xaxis_inverted()
assert ax.yaxis_inverted()

# 5. two shared axes. Clearing the master axis should bring axes in shared
# axes back to normal
# 5. two shared axes. Inverting the master axis should invert the shared
# axes; clearing the master axis should bring axes in shared
# axes back to normal.
ax0 = plt.subplot(211)
ax1 = plt.subplot(212, sharey=ax0)
ax0.imshow(img)
ax0.yaxis.set_inverted(True)
assert ax1.yaxis_inverted()
ax1.plot(x, np.cos(x))
ax0.cla()
assert not(ax1.yaxis_inverted())
assert not ax1.yaxis_inverted()
ax1.cla()
# 6. clearing the nonmaster should not touch limits
ax0.imshow(img)
Expand Down