Skip to content

Conversation

@philippjfr
Copy link
Contributor

This PR makes it possible to pass an existing matplotlib Axes object 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 the plt function calls. While that is a good fix for users using the plt interface, 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.

@philippjfr philippjfr changed the title Added handling of axes handling to regular plotting functions Added handling of axes to regular plotting functions Feb 17, 2016
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')
Copy link
Member

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

Copy link
Contributor Author

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.

@marqh
Copy link
Member

marqh commented Feb 17, 2016

you also need to change the copyright statement in the first line of plot.py

it needs to read

# (C) British Crown Copyright 2010 - 2016, Met Office

as you've changed it in 2016

@philippjfr
Copy link
Contributor Author

Thanks @marqh, will do that and will hopefully get the contributor agreement sorted out quickly. Any thoughts/recommendations on examples, documentation and tests?

@marqh
Copy link
Member

marqh commented Feb 17, 2016

i recommend
https://github.com/SciTools/iris/tree/master/lib/iris/tests/unit/plot
as the start point for testing

the unit tests (lib/iris/tests/unit/.../.py) are gradually replacing the older tests (lib/iris/tests/.py)

@marqh
Copy link
Member

marqh commented Feb 17, 2016

@philippjfr
Copy link
Contributor Author

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.

@marqh
Copy link
Member

marqh commented Feb 18, 2016

CLA received, with thanks
SciTools/scitools.org.uk#120

@marqh
Copy link
Member

marqh commented Feb 19, 2016

Hi @philippjfr

this looks grand. please add a stub to
https://github.com/philippjfr/iris/tree/master/docs/iris/src/whatsnew/contributions_1.10
and squish your commits

then this should be good to merge
thank you

@philippjfr
Copy link
Contributor Author

@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.

@philippjfr philippjfr force-pushed the master branch 2 times, most recently from c03e6ac to ffb30aa Compare February 19, 2016 21:32
@philippjfr
Copy link
Contributor Author

@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)
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Raising an exception now.

@philippjfr
Copy link
Contributor Author

@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):
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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'>

Copy link
Contributor Author

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.

@philippjfr
Copy link
Contributor Author

Fixed the double not bug above, which will hopefully fix the tests and added some further unit tests to ensure that plots actually raise the GeoAxes exception when passed a regular axes.

@philippjfr
Copy link
Contributor Author

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.

@philippjfr philippjfr force-pushed the master branch 2 times, most recently from 57ed2ed to ab2808a Compare March 1, 2016 12:23
@philippjfr
Copy link
Contributor Author

Resolved the failing tests, the logic for raising an exception was off. I think it's ready to merge now.

pelson added a commit that referenced this pull request Mar 2, 2016
Added handling of axes to regular plotting functions
@pelson pelson merged commit 808d208 into SciTools:master Mar 2, 2016
@pelson
Copy link
Member

pelson commented Mar 2, 2016

Thanks @philippjfr - this is an excellent addition. 👍

@QuLogic QuLogic added this to the v1.10 milestone Mar 2, 2016
@philippjfr
Copy link
Contributor Author

Yay! Thanks for merging @pelson.

pelson added a commit to pelson/iris that referenced this pull request Mar 2, 2016
pp-mo added a commit that referenced this pull request May 6, 2016
@rcomer rcomer mentioned this pull request Apr 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants