8000 Fix inversion of shared axes. · matplotlib/matplotlib@1255aa5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1255aa5

Browse files
committed
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 28fde25 commit 1255aa5

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
"""
@@ -2166,6 +2165,11 @@ def set_data_interval(self, vmin, vmax, ignore=False):
21662165
self.axes.dataLim.intervalx = min(vmin, Vmin), max(vmax, Vmax)
21672166
self.stale = True
21682167

2168+
def set_inverted(self, inverted):
2169+
# docstring inherited
2170+
a, b = self.get_view_interval()
2171+
self.axes.set_xlim(sorted((a, b), reverse=inverted), auto=None)
2172+
21692173
def set_default_intervals(self):
21702174
# docstring inherited
21712175
xmin, xmax = 0., 1.
@@ -2494,6 +2498,11 @@ def set_data_interval(self, vmin, vmax, ignore=False):
24942498
self.axes.dataLim.intervaly = min(vmin, Vmin), max(vmax, Vmax)
24952499
self.stale = True
24962500

2501+
def set_inverted(self, inverted):
2502+
# docstring inherited
2503+
a, b = self.get_view_interval()
2504+
self.axes.set_ylim(sorted((a, b), reverse=inverted), auto=None)
2505+
24972506
def set_default_intervals(self):
24982507
# docstring inherited
24992508
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)
0