Question

I really like the idea that cartopy can automatically plot in different map projections. However, I couldn't figure out how to do with the Iris cubes. As its a sister project, I expected that I might be able to. Is it possible to do something like this?

import iris as I
import cartopy.crs as ccrs
import matplotlib.pyplot as plt

someCube = I.load('someCube.pp')
ax = plt.axes(projection=ccrs.Robinson())
I.plot.contourf(someCube, transform=ccrs.Robinson())
plt.show()

thanks

Was it helpful?

Solution

I took your pseudo code and made it runnable with Iris' sample data:

import iris
import iris.plot as iplt
import cartopy.crs as ccrs
import matplotlib.pyplot as plt


fname = iris.sample_data_path('air_temp.pp')
air_temp = iris.load_cube(fname)

ax = plt.axes(projection=ccrs.Robinson())
iplt.contourf(air_temp, transform=ccrs.Robinson(central_longitude=180))
ax.coastlines()

plt.show()

If you run this code, you will get an exception along the lines of:

Traceback (most recent call last):
  File "using_custom_projections.py", line 11, in <module>
    iris.plot.contourf(air_temp, transform=ccrs.Robinson())
  File "lib/iris/plot.py", line 452, in contourf
    result = _draw_2d_from_points('contourf', None, cube, *args, **kwargs)
  File "lib/iris/plot.py", line 263, in _draw_2d_from_points
    result = _map_common(draw_method_name, arg_func, iris.coords.POINT_MODE, cube, data, *args, **kwargs)
  File "lib/iris/plot.py", line 406, in _map_common
    assert 'transform' not in kwargs, 'Transform keyword is not allowed.'
AssertionError: Transform keyword is not allowed.

Which is trying to tell you that you do not need to tell it which "transform" (or coordinate system) the cube is in. The reason for that is that an Iris cube should contain full metadata about the underlying data: the coordinate systems is part of that metadata.

So, to get the example to work, you can simply remove the transform keyword argument in your contourf call:

import iris
import iris.plot as iplt
import cartopy.crs as ccrs
import matplotlib.pyplot as plt


fname = iris.sample_data_path('air_temp.pp')
air_temp = iris.load_cube(fname)

ax = plt.axes(projection=ccrs.Robinson(central_longitude=180))
iplt.contourf(air_temp)
ax.coastlines()

plt.show()

contoured result

There is a similar example in the iris gallery, specifically http://scitools.org.uk/iris/docs/latest/examples/graphics/rotated_pole_mapping.html#rotated-pole-mapping-03 (the very last plot in the example).

HTH,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top