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

Skip to content

Added axes inversion to cla() #8455

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 14 commits into from
Apr 16, 2017
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
9 changes: 9 additions & 0 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,10 @@ def cla(self):
self.xaxis.set_minor_locator(minl)
else:
self.xaxis._set_scale('linear')
try:
self.set_xlim(0, 1)
except TypeError:
pass

if self._sharey is not None:
self.yaxis.major = self._sharey.yaxis.major
Expand All @@ -1020,6 +1024,10 @@ def cla(self):
self.yaxis.set_minor_locator(minl)
else:
self.yaxis._set_scale('linear')
try:
self.set_ylim(0, 1)
except TypeError:
pass

# update the minor locator for x and y axis based on rcParams
if (rcParams['xtick.minor.visible']):
Expand Down Expand Up @@ -1110,6 +1118,7 @@ def cla(self):
if self._sharey:
self.yaxis.set_visible(yaxis_visible)
self.patch.set_visible(patch_visible)

self.stale = True

@cbook.deprecated("2.1", alternative="Axes.patch")
Expand Down
48 changes: 48 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,54 @@ def test_twin_inherit_autoscale_setting():
assert not ax_y_off.get_autoscaley_on()


def test_inverted_cla():
# Github PR #5450. Setting autoscale should reset
# axes to be non-inverted.
# plotting an image, then 1d graph, axis is now down
fig = plt.figure(0)
ax = fig.gca()
# 1. test that a new axis is not inverted per default
assert not(ax.xaxis_inverted())
assert not(ax.yaxis_inverted())
img = np.random.random((100, 100))
ax.imshow(img)
# 2. test that a image axis is inverted
assert not(ax.xaxis_inverted())
assert ax.yaxis_inverted()
# 3. test that clearing and plotting a line, axes are
# not inverted
ax.cla()
x = np.linspace(0, 2*np.pi, 100)
ax.plot(x, np.cos(x))
assert not(ax.xaxis_inverted())
assert not(ax.yaxis_inverted())

# 4. autoscaling should not bring back axes to normal
ax.cla()
ax.imshow(img)
plt.autoscale()
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
ax0 = plt.subplot(211)
ax1 = plt.subplot(212, sharey=ax0)
ax0.imshow(img)
ax1.plot(x, np.cos(x))
ax0.cla()
assert not(ax1.yaxis_inverted())
ax1.cla()
# 6. clearing the nonmaster should not touch limits
ax0.imshow(img)
ax1.plot(x, np.cos(x))
ax1.cla()
assert ax.yaxis_inverted()

# clean up
plt.close(fig)


@image_comparison(baseline_images=["minorticks_on_rcParams_both"],
extensions=['png'])
def test_minorticks_on_rcParams_both():
Expand Down
6 changes: 5 additions & 1 deletion lib/mpl_toolkits/mplot3d/axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,7 @@ def cla(self):
# Disabling mouse interaction might have been needed a long
# time ago, but I can't find a reason for it now - BVR (2012-03)
#self.disable_mouse_rotation()
Axes.cla(self)
Copy link
Contributor Author

@jrmlhermitte jrmlhermitte Apr 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WeatherGod , curious (but not so important) question. Any reason why we're not using super(Axes3D, self).cla()? Since I'm on this line I could change to the newer method. (But I guess we shouldn't care unless we get into MRO details)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I once tried super-fying Axes3D (about 3 years ago). Some weird bugs started happening, so I dropped it. At the time, I was highly inexperienced with super(), so I might have been doing it wrong.

I am a firm believer in keeping PRs orthogonal. If you want to take a crack at super-fying Axes3D, you are more than welcome to try, but do so in a separate PR.

Copy link
Contributor Author

@jrmlhermitte jrmlhermitte Apr 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've battled with super quite a bit. If there is a bug, it usually has to do with Method Resolution Order (MRO) (I hope...). I think you know that already, but writing it in case anyone else is reading.

But that's something you need to worry about only when a class inherits from multiple parents. Multiple inheritance complicates things, if you just try to think about how you would do it if you had to using only super, you quickly see it's not that simple (and some convention must be chosen).

Also, I'm not sure if MRO changed from python 2x to 3x. I'll do some reading and see if it's worth doing. So I'll give it a shot (and keep it simple), and hope your bugs were to do with MRO...

PS : I think you know all this... but I'm not 100% sure. I remember this was a frustrating thing for me when I was first using multiple inheritance, but it ended up working nicely. So I figured it was worth a small discussion here.

self.zaxis.cla()

if self._sharez is not None:
Expand All @@ -1086,11 +1087,14 @@ def cla(self):
self.zaxis._set_scale(self._sharez.zaxis.get_scale())
else:
self.zaxis._set_scale('linear')
try:
self.set_zlim(0, 1)
except TypeError:
pass

self._autoscaleZon = True
self._zmargin = 0

Axes.cla(self)

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

Expand Down
20 changes: 20 additions & 0 deletions lib/mpl_toolkits/tests/test_mplot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,23 @@ def test_invalid_axes_limits(setter, side, value):
obj = fig.add_subplot(111, projection='3d')
with pytest.raises(ValueError):
getattr(obj, setter)(**limit)


def test_inverted_cla():
# Github PR #5450. Setting autoscale should reset
# axes to be non-inverted.
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
# 1. test that a new axis is not inverted per default
assert not ax.xaxis_inverted()
assert not ax.yaxis_inverted()
assert not ax.zaxis_inverted()
ax.set_xlim(1, 0)
ax.set_ylim(1, 0)
ax.set_zlim(1, 0)
assert ax.xaxis_inverted()
assert ax.yaxis_inverted()
assert ax.zaxis_inverted()
ax.cla()
assert not ax.xaxis_inverted()
assert not ax.yaxis_inverted()
assert not ax.zaxis_inverted()