Cartopy

Is a mapping and imaging package originating from the Met. Office in the UK. The home page for the package is http://scitools.org.uk/cartopy/. Like many python packages, the documentation is patchy and the best way to learn is to try to do things and ask other people who have figured out this and that.

We are going to work through a number of the examples and try to extend them to do the kinds of things you might find interesting and useful in the future. The examples are in the form of a gallery

You might also want to look at the list of map projections from time to time. Not all maps can be plotted in every projection (sometimes because of bugs and sometimes because they are not supposed to work for the data you have) but you can try them and see what happens.

Cartopy is built on top of a lot of the matplotlib graphing tools. It works by introducing a series of projections associated with the axes of a graph. On top of that there is a big toolkit for reading in images, finding data from standard web feeds, and manipulating geographical objects. Many, many libraries are involved and sometimes things break. Luckily the installation that is built for this course is about as reliable as we can ever get. I'm just warning you, though, that it can be quite tough if you want to put this on your laptop from scratch.

Let's get started

We have a number of imports that we will need almost every time.

If we are going to plot anything then we need to include matplotlib.


In [1]:
%pylab inline

import matplotlib.pyplot as plt

import cartopy
import cartopy.crs as ccrs


Populating the interactive namespace from numpy and matplotlib

In [2]:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt

ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()
ax.coastlines()


Out[2]:
<cartopy.mpl.feature_artist.FeatureArtist at 0x108ce9090>

The simplest plot: global map using the default image built into the package and adding coastlines


In [3]:
fig = plt.figure(figsize=(12, 12), facecolor="none")
ax  = plt.axes(projection=ccrs.Mercator())

    # make the map global rather than have it zoom in to
    # the extents of any plotted data
    
ax.set_global()
ax.coastlines()  
ax.stock_img()


Out[3]:
<matplotlib.image.AxesImage at 0x1097c4cd0>

Try changing the projection - either look at the list in the link I gave you above or use the tab-completion feature of iPython to see what ccrs has available ( not everything will be a projection, but you can see what works and what breaks ).

Here is how you can plot a region instead of the globe:


In [4]:
fig = plt.figure(figsize=(12, 12), facecolor="none")
ax  = plt.axes(projection=ccrs.Robinson())    
ax.set_extent([0, 40, 28, 48])

ax.coastlines(resolution='50m')  
ax.stock_img()


Out[4]:
<matplotlib.image.AxesImage at 0x1045e88d0>

In [5]:
help(ax.stock_img)


Help on method stock_img in module cartopy.mpl.geoaxes:

stock_img(self, name='ne_shaded') method of cartopy.mpl.geoaxes.GeoAxesSubplot instance
    Add a standard image to the map.
    
    Currently, the only (and default) option is a downsampled version of
    the Natural Earth shaded relief raster.


In [ ]: