-
Notifications
You must be signed in to change notification settings - Fork 300
y-axis inversion for vertical coords #522
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'm in favour of this in principle. However, I'm not sure about the logic that's been used. For example, if I'm plotting a 'down' coordinate on the horizontal axis do I want to invert the y-axis? I do not believe so. Do I want to invert the x-axis? I'm not sure. |
|
This is true. It might well be worth restricting it to 'only invert the y-axis if the down coordinate is being plotted on the y-axis'. However if ever a y-like coordinate needed to be plotted inverted on the x-axis this suggestion would clearly prevent that. I'll investigate whether such a y-like coordinate would ever need to be plotted on the x-axis. |
I think so. I also think you would want the positive attribute to apply to both the x and y if your x and y coordinates both define a positive attribute... |
|
I asked the scientist whose request originally prompted this work about whether the x-axis would ever need to be inverted and his view is that it shouldn't. Further, according to the CF conventions doc, section 1.3; "Other vertical coordinates must use the attribute Of course if it is needed there is nothing to stop the x-axis being inverted manually, which I think might prove a more appropriate way to go forward with this. |
lib/iris/tests/test_cf.py
Outdated
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.
This is a little strict. Perhaps test:
self.assertEquals(cube.coord('height').attributes['positive'], 'up')
I think I agree with @dkillick on this one -- not to invert a X axis. ( Sorry if this repeats, I wrote before seeing @dkillick latest ! ) |
lib/iris/tests/test_plot.py
Outdated
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.
You said that the CF spec states that this is case insensitive, perhaps you could change this to:
cube.attributes['positive'] = 'DOWN' and make the necessary changes to plot.py.
|
You seem to have a couple of test images that correspond to an earlier commit: |
|
I'm confused about a cube having a positive attribute, does this make sense? Looking at your test cases for 1D plots, the positive attribute causes the temperature axis to be inverted, when surely the intention is that the depth coordinate should be reversed? I would have thought you should still be checking for a positive attribute on the coordinate not the cube. Please clarify. Also, wouldn't it make more sense to plot depth on the y-axis for these examples? It concerns me that the y-axis is reversed when it probably is not intended to be. |
|
Thanks for your comments @ajdawson and apologies for the radio silence at my end - I'm working on another project currently that's proving quite time-intensive! In terms of your comments, firstly the cube does not under normal circumstances gain a positive attribute. Only a y-like coordinate will gain that attribute, unless, as in the case of this test, the attribute is forced. Second, the inversion of the temperature axis in this case is actually the appropriate behaviour because clearly in the 1D case the data plotted on the y-axis is actually the data within the dramatically constrained cube. This is then being set to be plotted inverted, which is indeed what is happening. Further, I chose the specific data here because the x-axis data has positive=down so it also (admittedly implicitly) tests that the x-axis is not inverted in this case. Hopefully this answers your concerns. |
OK that makes things clearer. I just think the test images are a little confusing. Is it actually possible to make a 1D plot of the same cube but with depth on the y-axis in iris? |
Perhaps this can't be done using |
|
I agree they could be better, but unfortunately I was a little restricted as to what data I could use as test data, hence the decision as laid out above. So |
|
I guess my point is that if this is supposed to apply to vertical coordinates only (seems fine) then there should be a way to plot a depth profile, so exactly the same data you have used but with depth on the y-axis. If this really is not possible with |
|
OK I've thought about this some more and here is where I stand:
The goal of the PR is to support inversion for vertical coordinates. Since one cannot make a plot of a 1D cube with the coordinate on the y-axis anayway, it does not make sense to support inverting the data axis by having a positive attribute on the cube. As you said, under normal circumstances the cube will not gain a positive attribute, so what is the functionality catering for? I propose that the 2D plotting aspects of this PR remain as they are, but the 1D aspects should be removed until such time that iris is capable of plotting a 1D cube with the coordinate on the y-axis. When this happens it will make sense to invert the coordinate that will be on the y-axis. |
👍 Sounds good. |
|
@dkillick since #593 is now merged it should be possible for this to be extended to 1D plots in a way that makes sense. |
|
@dkillick a further thought occurred to me: what happens if the user makes two plots on the same axes, both of which are of variables that have a vertical coordinate with a positive='down' attribute? For example, if I have two cubes which represent say zonal velocity in the ocean and an anomalous zonal velocity, both of which have a vertical coordinate 'depth' with the positive='down' attribute. I want to plot line contours of the anomaly over the full field: fill = contourf(velocity, coords=['latitude', 'depth'])
line = contour(velocity_anomaly, coords=['latitude', 'depth'])The first call will correctly invert the y-axis, but won't the second also detect the vertical coordinate should be inverted and end up undoing the inversion? |
|
Good call @ajdawson! I just had a play with a simple matplotlib plot and you're quite right - as Thus I propose that I will add a test to the code that does the to check whether the y-axis is already inverted. Something like |
Jolly good then! 😄 |
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.
Does this cube have a vertical coordinate with a positive=down attribute?
Edit: sorry of course it does, I'm doing something wrong at my end!
|
The 1d positive=down test image isn't correct, it should have depth on the y-axis according to the test case but the image has temperature on the y-axis and it is inverted... Can you check this please. |
|
Oh, so it is... now sorted. I didn't notice because the tests did not fail... |
I don't see any new commits or other changes to the branch... |
|
Indeed not, I was just sorting out the potential lower case issue highlighted above. Commit to follow soon... |
|
Sorry I thought you meant you had already done it. |
|
@dkillick - you've got a PEP8 failure. |
lib/iris/plot.py
Outdated
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.
Missing full stop.
|
FYI @dkillick just adding commits to a PR doesn't seem to generate a notification, so add a comment if you want people to know that you have made changes and are ready for re-review. I'd like to have a quick play with it tomorrow to double check in a real-world setting so to speak, but it looks ready for merge. |
|
Interesting, I wasn't aware of that. Thanks for letting me know @ajdawson! |
|
@dkillick - can you check if this breaks any of the example tests. I think it should cause the first plot in the |
|
@ajdawson I just ran the example tests on my machine here and they all passed, so I had a look running the code manually and the first plot is indeed plotting so that the y-axis has 0 at the bottom again. This must be because when plot runs it will invert the y-axis which is then inverted again by the y-axis invert command later on in the example code. So I commented that line and re-ran the tests... no failure. Seems I'm seeing the same behaviour as you are. |
|
@dkillick - OK that is sort of what I expected. I think this PR should remove the manual inversion line from that example so that the gallery will build correctly, lets not worry about the test for now as I think it is a much deeper issue. Perhaps you could also add a short comment in the example to say the axis will be automatically inverted due to its positive=down attribute? After that I think this one is good to go. Are you able to work on it today to get it in before the 1.5.x branch is cut? It would be good to get this in. |
|
Those pesky thresholds 😉. @ajdawson is right - drop the manual inversion and let's get this merged. |
|
@dkillick - looks good. Can you squash these commits into one please. There were some travis fails that look related to recently merged PRs. I'm not exactly sure how these errors came about but could you also rebase onto the current master while you are at it to try and make these go away? |
|
I think those failures may be my fault. |
|
Rebased, squashed, pushed. Just awaiting Travis results now... |
|
Not having much luck with Travis on this PR! I'm not sure what caused that error, perhaps just a Travis glitch, I'll try and restart it. |
|
I just did a local merge and ran all three sets of tests. Everything passes and the depth axis looks good in the example so 👍. |
y-axis inversion for vertical coords
|
Thanks @dkillick, good work. |
|
Got there at last 😄 |
A cf attribute that can be set on vertical coordinates is 'positive', which indicates the direction in which they should be plotted to retain visual meaning. Thus positive = up indicates 0 should be plotted at the bottom of the y-axis as standard while positive = down indicates that 0 should be plotted at the top of the y-axis. This is beneficial when plotting phenomena such as air pressure or depth, both of which intuitively increase downward.
Iris plotting methods did not handle this coordinate however, which is what this PR changes. To accomplish this, I modified netcdf.py to allow the positive attribute to be added to coords on building a cube from an nc file. I then modified plot.py to pick up this attribute such that if it was found on a coordinate to be plotted then the y-axis is inverted.
I modified the tests to pass: adding a test to test_netcdf.py for the presence of positive on a dimcoord known to have it set; adding a test class to test_plot.py to test that positive = up/down are being plotted correctly; changing the cml files affected by the presence of a new attribute within a dimcoord.