-
Notifications
You must be signed in to change notification settings - Fork 300
Added handling of axes to regular plotting functions #1942
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
lib/iris/plot.py
Outdated
| u, v = plot_arrays | ||
| u, v = _broadcast_2d(u, v) | ||
| draw_method = getattr(plt, draw_method_name) | ||
| axes = kwargs.pop('axes') |
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.
there will need to be a None returned if axes doesn't exist
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 catching that, will make any fixes once you're done reviewing.
|
you also need to change the copyright statement in the first line of plot.py it needs to read as you've changed it in 2016 |
|
Thanks @marqh, will do that and will hopefully get the contributor agreement sorted out quickly. Any thoughts/recommendations on examples, documentation and tests? |
|
i recommend the unit tests (lib/iris/tests/unit/.../.py) are gradually replacing the older tests (lib/iris/tests/.py) |
|
I think a lot of the plot types in will want this behaviour I think that we should explicitly mention 'axes' on any plot type that we have implemented the change for |
Agreed, I think they should all support it now, so I'll just make sure to document the kwarg everywhere. |
|
CLA received, with thanks |
|
Hi @philippjfr this looks grand. please add a stub to then this should be good to merge |
|
@marqh Thanks for the review, I'll make sure to do that. I've also looked into adding some unit tests based on the existing unit tests but as far as I can tell only the contour and contourf calls are actually setting the x- and y-ticks correctly, so give me some time to investigate that. |
c03e6ac to
ffb30aa
Compare
|
@marqh Made some final fixes, added unit tests, added an entry under contributions and squashed the commits. I think it's ready to merge, but let me know if you want me to refactor those unit tests a little bit. |
| # Ensure the current axes are a cartopy.mpl.GeoAxes instance. | ||
| _replace_axes_with_cartopy_axes(cartopy_proj) | ||
| if not kwargs.get('axes'): | ||
| _replace_axes_with_cartopy_axes(cartopy_proj) |
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 we can make this conditional - what if you've made an axes which isn't a cartopy GeoAxes and passed that through... it may be that we need to raise an exception in that situation.
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.
Good point, raising an exception makes sense to me.
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.
Raising an exception now.
c95232f to
2888005
Compare
|
@pelson I've finally made the revisions you asked for, except regarding your comment to attach the anchored text to the figure. I'd be happy to revert adding an axes argument there for for now, just let me know what you think is best. |
lib/iris/plot.py
Outdated
| _replace_axes_with_cartopy_axes(cartopy_proj) | ||
| if 'axes' not in kwargs: | ||
| _replace_axes_with_cartopy_axes(cartopy_proj) | ||
| elif not not isinstance(kwargs['axes'], cartopy.mpl.geoaxes.GeoAxes): |
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.
double not. I'm not not sure what happens here...
This highlights the need for a test of this error I think.
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.
Agreed, will add one now.
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.
Looks like some of the tests are failing because of this too:
======================================================================
ERROR: test_outline (iris.tests.test_mapping.TestLimitedAreaCube)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/travis/miniconda/envs/test-environment/lib/python2.7/site-packages/Iris-1.10.0.dev0-py2.7-linux-x86_64.egg/iris/tests/test_mapping.py", line 240, in test_outline
iplt.outline(self.cube)
File "/home/travis/miniconda/envs/test-environment/lib/python2.7/site-packages/Iris-1.10.0.dev0-py2.7-linux-x86_64.egg/iris/plot.py", line 845, in outline
antialiased=True, coords=coords, axes=axes)
File "/home/travis/miniconda/envs/test-environment/lib/python2.7/site-packages/Iris-1.10.0.dev0-py2.7-linux-x86_64.egg/iris/plot.py", line 244, in _draw_2d_from_bounds
cube, plot_defn, *args, **kwargs)
File "/home/travis/miniconda/envs/test-environment/lib/python2.7/site-packages/Iris-1.10.0.dev0-py2.7-linux-x86_64.egg/iris/plot.py", line 614, in _map_common
return plotfn(*new_args, **kwargs)
File "/home/travis/miniconda/envs/test-environment/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2946, in pcolormesh
ret = ax.pcolormesh(*args, **kwargs)
File "/home/travis/miniconda/envs/test-environment/lib/python2.7/site-packages/matplotlib/axes.py", line 7766, in pcolormesh
t = t._as_mpl_transform(self.axes)
File "lib/cartopy/_crs.pyx", line 213, in cartopy._crs.CRS._as_mpl_transform (lib/cartopy/_crs.c:4071)
ValueError: Axes should be an instance of GeoAxes, got <class 'matplotlib.axes.AxesSubplot'>
======================================================================
ERROR: test_grid (iris.tests.test_mapping.TestBoundedCube)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/travis/miniconda/envs/test-environment/lib/python2.7/site-packages/Iris-1.10.0.dev0-py2.7-linux-x86_64.egg/iris/tests/test_mapping.py", line 205, in test_grid
iplt.outline(self.cube)
File "/home/travis/miniconda/envs/test-environment/lib/python2.7/site-packages/Iris-1.10.0.dev0-py2.7-linux-x86_64.egg/iris/plot.py", line 845, in outline
antialiased=True, coords=coords, axes=axes)
File "/home/travis/miniconda/envs/test-environment/lib/python2.7/site-packages/Iris-1.10.0.dev0-py2.7-linux-x86_64.egg/iris/plot.py", line 244, in _draw_2d_from_bounds
cube, plot_defn, *args, **kwargs)
File "/home/travis/miniconda/envs/test-environment/lib/python2.7/site-packages/Iris-1.10.0.dev0-py2.7-linux-x86_64.egg/iris/plot.py", line 614, in _map_common
return plotfn(*new_args, **kwargs)
File "/home/travis/miniconda/envs/test-environment/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2946, in pcolormesh
ret = ax.pcolormesh(*args, **kwargs)
File "/home/travis/miniconda/envs/test-environment/lib/python2.7/site-packages/matplotlib/axes.py", line 7766, in pcolormesh
t = t._as_mpl_transform(self.axes)
File "lib/cartopy/_crs.pyx", line 213, in cartopy._crs.CRS._as_mpl_transform (lib/cartopy/_crs.c:4071)
ValueError: Axes should be an instance of GeoAxes, got <class 'matplotlib.axes.AxesSubplot'>
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.
Likely due to the double not above. Fixing that and adding new unit tests now.
|
Fixed the double |
|
Seems there are further tests failing due the new GeoAxes exception. I don't quite understand how that's happening unless those tests have been passing axes in all along and they were simply ignored until now. Will investigate. |
57ed2ed to
ab2808a
Compare
|
Resolved the failing tests, the logic for raising an exception was off. I think it's ready to merge now. |
Added handling of axes to regular plotting functions
|
Thanks @philippjfr - this is an excellent addition. 👍 |
|
Yay! Thanks for merging @pelson. |
This PR makes it possible to pass an existing matplotlib
Axesobject to the various plotting functions in the plotting module. I realize that in PR #1112 drawing directly onto the axes was disabled, instead always drawing to the axis that is currently open using thepltfunction calls. While that is a good fix for users using thepltinterface, which is useful for simple usage, when building more complex plots the object-oriented approach is the way to go. This is crucial for some of the functionality we're building as part of the cube-explorer project integrating Iris functionality within HoloViews.It's a fairly small set of changes but not being familiar with the code I may have missed something. I think only providing this functionality in plot but not quickplot makes sense since the latter is suited towards usage in an interactive interpreter anyway. I'd be happy to add examples, documentation and tests once we agree these are the correct changes.