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

Skip to content

Commit 7ce91c6

Browse files
authored
Merge pull request #8455 from ordirules/axisinvert
Added axes inversion to cla()
2 parents 1c0b96c + a2f7ed9 commit 7ce91c6

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,10 @@ def cla(self):
997997
self.xaxis.set_minor_locator(minl)
998998
else:
999999
self.xaxis._set_scale('linear')
1000+
try:
1001+
self.set_xlim(0, 1)
1002+
except TypeError:
1003+
pass
10001004

10011005
if self._sharey is not None:
10021006
self.yaxis.major = self._sharey.yaxis.major
@@ -1020,6 +1024,10 @@ def cla(self):
10201024
self.yaxis.set_minor_locator(minl)
10211025
else:
10221026
self.yaxis._set_scale('linear')
1027+
try:
1028+
self.set_ylim(0, 1)
1029+
except TypeError:
1030+
pass
10231031

10241032
# update the minor locator for x and y axis based on rcParams
10251033
if (rcParams['xtick.minor.visible']):
@@ -1110,6 +1118,7 @@ def cla(self):
11101118
if self._sharey:
11111119
self.yaxis.set_visible(yaxis_visible)
11121120
self.patch.set_visible(patch_visible)
1121+
11131122
self.stale = True
11141123

11151124
@cbook.deprecated("2.1", alternative="Axes.patch")

lib/matplotlib/tests/test_axes.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,54 @@ def test_twin_inherit_autoscale_setting():
178178
assert not ax_y_off.get_autoscaley_on()
179179

180180

181+
def test_inverted_cla():
182+
# Github PR #5450. Setting autoscale should reset
183+
# axes to be non-inverted.
184+
# plotting an image, then 1d graph, axis is now down
185+
fig = plt.figure(0)
186+
ax = fig.gca()
187+
# 1. test that a new axis is not inverted per default
188+
assert not(ax.xaxis_inverted())
189+
assert not(ax.yaxis_inverted())
190+
img = np.random.random((100, 100))
191+
ax.imshow(img)
192+
# 2. test that a image axis is inverted
193+
assert not(ax.xaxis_inverted())
194+
assert ax.yaxis_inverted()
195+
# 3. test that clearing and plotting a line, axes are
196+
# not inverted
197+
ax.cla()
198+
x = np.linspace(0, 2*np.pi, 100)
199+
ax.plot(x, np.cos(x))
200+
assert not(ax.xaxis_inverted())
201+
assert not(ax.yaxis_inverted())
202+
203+
# 4. autoscaling should not bring back axes to normal
204+
ax.cla()
205+
ax.imshow(img)
206+
plt.autoscale()
207+
assert not(ax.xaxis_inverted())
208+
assert ax.yaxis_inverted()
209+
210+
# 5. two shared axes. Clearing the master axis should bring axes in shared
211+
# axes back to normal
212+
ax0 = plt.subplot(211)
213+
ax1 = plt.subplot(212, sharey=ax0)
214+
ax0.imshow(img)
215+
ax1.plot(x, np.cos(x))
216+
ax0.cla()
217+
assert not(ax1.yaxis_inverted())
218+
ax1.cla()
219+
# 6. clearing the nonmaster should not touch limits
220+
ax0.imshow(img)
221+
ax1.plot(x, np.cos(x))
222+
ax1.cla()
223+
assert ax.yaxis_inverted()
224+
225+
# clean up
226+
plt.close(fig)
227+
228+
181229
@image_comparison(baseline_images=["minorticks_on_rcParams_both"],
182230
extensions=['png'])
183231
def test_minorticks_on_rcParams_both():

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,7 @@ def cla(self):
10761076
# Disabling mouse interaction might have been needed a long
10771077
# time ago, but I can't find a reason for it now - BVR (2012-03)
10781078
#self.disable_mouse_rotation()
1079+
Axes.cla(self)
10791080
self.zaxis.cla()
10801081

10811082
if self._sharez is not None:
@@ -1086,11 +1087,14 @@ def cla(self):
10861087
self.zaxis._set_scale(self._sharez.zaxis.get_scale())
10871088
else:
10881089
self.zaxis._set_scale('linear')
1090+
try:
1091+
self.set_zlim(0, 1)
1092+
except TypeError:
1093+
pass
10891094

10901095
self._autoscaleZon = True
10911096
self._zmargin = 0
10921097

1093-
Axes.cla(self)
10941098

10951099
self.grid(rcParams['axes3d.grid'])
10961100

lib/mpl_toolkits/tests/test_mplot3d.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,3 +564,23 @@ def test_invalid_axes_limits(setter, side, value):
564564
obj = fig.add_subplot(111, projection='3d')
565565
with pytest.raises(ValueError):
566566
getattr(obj, setter)(**limit)
567+
568+
569+
def test_inverted_cla():
570+
# Github PR #5450. Setting autoscale should reset
571+
# axes to be non-inverted.
572+
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
573+
# 1. test that a new axis is not inverted per default
574+
assert not ax.xaxis_inverted()
575+
assert not ax.yaxis_inverted()
576+
assert not ax.zaxis_inverted()
577+
ax.set_xlim(1, 0)
578+
ax.set_ylim(1, 0)
579+
ax.set_zlim(1, 0)
580+
assert ax.xaxis_inverted()
581+
assert ax.yaxis_inverted()
582+
assert ax.zaxis_inverted()
583+
ax.cla()
584+
assert not ax.xaxis_inverted()
585+
assert not ax.yaxis_inverted()
586+
assert not ax.zaxis_inverted()

0 commit comments

Comments
 (0)