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

Skip to content

Commit 0e95296

Browse files
anntzerivanov
authored andcommitted
Fix inversion of shared axes.
set_view_interval does not update shared axes, we must use set_xlim/set_ylim to do so. Update a test to explicitly invert an axis instead of relying on imshow to implicitly do so.
1 parent c8b6105 commit 0e95296

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

lib/matplotlib/axis.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,11 +1028,10 @@ def set_inverted(self, inverted):
10281028
the top for the y-axis; the "inverse" direction is increasing to the
10291029
left for the x-axis and to the bottom for the y-axis.
10301030
"""
1031-
a, b = self.get_view_interval()
1032-
if inverted:
1033-
self.set_view_interval(max(a, b), min(a, b), ignore=True)
1034-
else:
1035-
self.set_view_interval(min(a, b), max(a, b), ignore=True)
1031+
# Currently, must be implemented in subclasses using set_xlim/set_ylim
1032+
# rather than generically using set_view_interval, so that shared
1033+
# axes get updated as well.
1034+
raise NotImplementedError('Derived must override')
10361035

10371036
def set_default_intervals(self):
10381037
"""
@@ -2171,6 +2170,11 @@ def get_ticks_position(self):
21712170
def get_minpos(self):
21722171
return self.axes.dataLim.minposx
21732172

2173+
def set_inverted(self, inverted):
2174+
# docstring inherited
2175+
a, b = self.get_view_interval()
2176+
self.axes.set_xlim(sorted((a, b), reverse=inverted), auto=None)
2177+
21742178
def set_default_intervals(self):
21752179
# docstring inherited
21762180
xmin, xmax = 0., 1.
@@ -2473,6 +2477,11 @@ def get_ticks_position(self):
24732477
def get_minpos(self):
24742478
return self.axes.dataLim.minposy
24752479

2480+
def set_inverted(self, inverted):
2481+
# docstring inherited
2482+
a, b = self.get_view_interval()
2483+
self.axes.set_ylim(sorted((a, b), reverse=inverted), auto=None)
2484+
24762485
def set_default_intervals(self):
24772486
# docstring inherited
24782487
ymin, ymax = 0., 1.

lib/matplotlib/tests/test_axes.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,19 @@ def test_inverted_cla():
240240
ax.cla()
241241
ax.imshow(img)
242242
plt.autoscale()
243-
assert not(ax.xaxis_inverted())
243+
assert not ax.xaxis_inverted()
244244
assert ax.yaxis_inverted()
245245

246-
# 5. two shared axes. Clearing the master axis should bring axes in shared
247-
# axes back to normal
246+
# 5. two shared axes. Inverting the master axis should invert the shared
247+
# axes; clearing the master axis should bring axes in shared
248+
# axes back to normal.
248249
ax0 = plt.subplot(211)
249250
ax1 = plt.subplot(212, sharey=ax0)
250-
ax0.imshow(img)
251+
ax0.yaxis.set_inverted(True)
252+
assert ax1.yaxis_inverted()
251253
ax1.plot(x, np.cos(x))
252254
ax0.cla()
253-
assert not(ax1.yaxis_inverted())
255+
assert not ax1.yaxis_inverted()
254256
ax1.cla()
255257
# 6. clearing the nonmaster should not touch limits
256258
ax0.imshow(img)

0 commit comments

Comments
 (0)