-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
added axes inversion to cla() #5450
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
Conversation
👍 I think it is definitely reasonable to expect clearing the reset the inversion. I think we should add a test for this. Something like do a plot to an inverted axis. Check that the axis is inverted, clear, do a new plot and check that the axis is not inverted. |
I am a bit more nuanced. I think it should clear so long as autoscaling is On Mon, Nov 9, 2015 at 11:25 AM, Jens Hedegaard Nielsen <
|
Thanks @jenshnielsen . I like @WeatherGod 's idea. How about not setting: self._autoscaleXon = True in the axes clear and then checking to see whether or not autoscale is set before clearing? I'll check later to see whether this is the right place to do it. |
@@ -1060,6 +1060,10 @@ def cla(self): | |||
self.yaxis.set_visible(yaxis_visible) | |||
self.patch.set_visible(patch_visible) | |||
self.stale = True | |||
if(self.xaxis_inverted()): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to do this without the tests?
These should go above setting self.stale = True
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the way to do this is in each of the "else" blocks of the "if sharex", sharey conditionals, add self.viewLim.intervalx = (0, 1)
etc. The actual values won't matter, only the fact that they are in the normal order.
Thanks @efiring. I have done this and it works wonderfully and it's a clever way to avoid the test, while taking into account the shared axes (oops!). I wasn't aware of this piece until now (never used them before), thanks for the patience in dealing with a "noob". ;-) @jenshnielsen you mentioned a test. Do you mean adding a test routine somewhere in the library itself? I am not familiar with this. Here is a proposed test for now, sorry it is lengthy. It also tests shared axes. import numpy as np
import matplotlib.pyplot as plt
# comment out for non-interactive plotting
plt.ion()
# figure 0 should look like figure 2, i.e.
# plotting should be independent of clearing history
# this specifically tests that the axes directions
# are independent of history
# plotting 1 d graph, positive is up
plt.figure(0);
x = np.linspace(0,2*np.pi,100);
plt.plot(x,np.cos(x))
# plotting a figure positive is down
plt.figure(1);
img = np.random.random((100,100))
plt.imshow(img)
# plotting an image, then 1d graph, axis is now down
plt.figure(2);
img = np.random.random((100,100))
plt.imshow(img)
plt.cla()
x = np.linspace(0,2*np.pi,100);
plt.plot(x,np.cos(x))
plt.autoscale()
# ensure that vice versa also not affected
plt.figure(3);
x = np.linspace(0,2*np.pi,100);
plt.plot(x,np.cos(x))
plt.cla()
img = np.random.random((100,100))
plt.imshow(img)
# try shared y-axis and clear one figure whose axis is shared,
# axes are not flipped back (expected result)
plt.figure(4);
ax1= plt.subplot(211);
plt.imshow(img)
plt.subplot(212,sharey=ax1);
plt.imshow(img)
plt.cla();
plt.plot(x,100*np.cos(x))
# clear the master shared axis
plt.figure(5);
ax1= plt.subplot(211);
plt.imshow(img)
plt.subplot(212,sharey=ax1);
plt.imshow(img)
plt.subplot(211);
plt.cla();
plt.plot(x,100*np.cos(x)) |
@ordirules The test for cla() in axes3d is failing, so something will need to be changed there. |
@ordirules I think you are on the right track. The test would most likely go into https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/tests/test_axes.py have a look at these tests and the resources at http://matplotlib.org/devel/testing.html A test could look like this: @cleanup
from nose.tools import assert_false
def test_inverted_cla():
# plotting an image, then 1d graph, axis is now down
fig, ax = plt.subplots()
# 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)
# test that a image axis is inverted
assert not(ax.xaxis_inverted())
assert ax.yaxis_inverted()
ax.cla()
x = np.linspace(0,2*np.pi,100);
ax.plot(x,np.cos(x))
# test if the axis are inverted
# assert something
plt.autoscale()
# test if the axis are inverted
# assert something We use assert to test if something is true or false. We could also test for the axis limits |
Yeah, this patch totally breaks cla() for axes3d. Essentially, axes3d assumes that viewLims is never mucked with directly. It probably would be better to utilize set_xlim/set_ylim instead in a manner similar above in the sharex/y condition. It would also be a good idea to add similar code to set_zlim() in axes3d.py as well. |
thanks this all sounds interesting. will take a look later, currently pretty busy. thanks I'm learning a lot about this lib :-). |
I would plead for not making this more complicated than really necessary. |
sorry, I have been incredibly late on this, pretty busy. Anyway, now I understand what nosetests are :-) I have added tests for the following:
@WeatherGod I have replaced the change of the viewLims to "set_xlim" and "set_ylim." since the axes are cleared, I just set them to (0,1) as mentioned before. I could grab the xlim and ylims but that adds extra code and overhead. Thanks @efiring , honestly, this was your idea and it seems to work great so far :-). One more comment to @WeatherGod regarding axes3d. I would rather not touch them (or make it a separate PR) since this adds an extra layer of complexity and I am not too familiar with it. What do you think? |
huh, we are testing some interesting projections in the test suite that do not liken @ordirules just made the connection that I know you in other contexts, sorry about that. |
Yeah, looks like it's line 168 of geo.py of this current commit, throwing an error, I'll paste the code here just to make it easier to discuss: def set_xlim(self, *args, **kwargs):
raise TypeError("It is not possible to change axes limits "
"for geographic projections. Please consider "
"using Basemap or Cartopy.") Perhaps should the new projections override cla() then? I think cla() as was mentioned before bring the axes back to their original state. I am by no means an expert on this, but thought I'd throw this out. @tacaswell no worries, great hackathon you organized btw, was a success :-) |
Another option is to |
that sounds much simpler. let's try it and see |
# 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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why this file isn't PEP8-tested, but there should be spaces after commas and around operators. Also, please remove the useless semicolons.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty sure we do not pep8 test the tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah okay, but they will be when the pytest PR is merged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the comment, I have this bad habit of putting semicolons (I squash multiple commands in a line when testing then forget to remove after unsquashing)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't touch axes3d, then an issue should be filed so we don't forget
to fix it, but I really think it should be quite straight-forward.
On Dec 24, 2015 10:58 AM, "Julien Lhermitte" [email protected]
wrote:
In lib/matplotlib/tests/test_axes.py
#5450 (comment):@@ -135,6 +135,48 @@ def test_twinx_cla():
assert_true(ax.patch.get_visible())
assert_true(ax.yaxis.get_visible())+@cleanup
+def test_inverted_cla():
Github PR added axes inversion to cla() #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()
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))
thanks for the comment, I have this bad habit of putting semicolons (I
squash multiple commands in a line when testing then forget to remove after
unsquashing)—
Reply to this email directly or view it on GitHub
https://github.com/matplotlib/matplotlib/pull/5450/files#r48419484.
I am closing this as I think it's a matter of preference. For the record, I get around this by using Just an opinion, sorry for keeping this open so long, I have been busy with other things. Thanks for your support. |
I do not think there was any disagreement about doing this. It seems like the only outstanding work was to add the same 3 lines in the 3D code. |
oops I did not see @WeatherGod 's comment on the outdated diff. |
@tacaswell moved to #8455 |
This has been bothering me a little. When axes are cleared, the x/y-axis should have the positive direction consistently point the same way.
Here is sample code:
Upon clearing, the yaxis is inverted. This is because plotting the image inverts the y axis since the origin is located in the upper left hand corner. If not image was previously plotted to the figure, the y axis would not have been inverted. I have tested it and it works nicely.
What do you think? Is this worth adding? As a user, I can definitely say this is a feature that would greatly simplify my life.
I'm pinging @fariza and @OceanWolf since they reviewed a pull request touching similar functions. (@OceanWolf, still have not done the div by zero checking but will later)
thanks for reading!