Plotting maps with cartopy and iris

Here are some notes for making maps using cartopy and iris. As with many things, it’s easy to make simple maps, and can be pretty complicated to make something just how you want it, so here we are.

WordPress’ own code embedding broke if there were any < or > symbols, so I’m using gists to embed the code snippets. The full, executable python scripts are also on github proper.

Simple sketch maps

Cartopy deals with the business of making maps, and we can make some nice-looking simple maps without too much trouble.

First, let’s get set up!

The simplest case: Create a GeoAxes object by providing a projection, automatically creating a Figure as necessary; and just plot the coastlines.

Make it a bit nicer: Use a different projection (shifted slightly), use a stock image without drawing the coastlines, and automatically adjust the margins:

Instead of the stock image, we can simply shade the land and oceans (let’s try another projection while we’re at it):

A final example:

Other features include BORDERS, LAKES, RIVERS; see the Cartopy feature interface for more details.

Demonstrating some projections

Here’s a more detailed example of some projections.

(The gridlines bit is a bit fiddly, but I’ll talk more about that below in the iris section.)


Plotting gridded data

This is where iris comes in.

So, starting again, let’s get set up, and load some data:

Quick, simple examples

This is handy for just making a quick and simple exploration of your data. We just need the one package, iris quickplot:

The simplest possible case is just one line:

But really we’ll need coastlines. We can also use pcolormesh and other similar commands, not just contourf:

(Note that contourf has discrete levels by default, whereas pcolormesh defaults to a continuous range.)

Quickplot plots are still pretty customizable on their own:

Set the levels explicitly (we’ll require numpy from here)

We can write that out a bit more explicitly… and while we’re at it, let’s make sure the range is symmetric about zero and use a more suitable, diverging colour scheme:

Mega Example

This has got all the “usual” steps in making a nice plot. So it’s a bit massive and complicated at times… I usually end up bunging a lot of this sort of thing into a function!

Start by importing the packages, and loading some data!

Set the value range, which gives us the levels to use in the plot:

Set up the colour palette. We’ll choose the colour scheme, including the out-of-range colours (although these shouldn’t be used if we’ve picked the value range well!), and how these are related to the value levels we’ve just defined.

Start the plot:

Set the domain of the plot, if necessary:
(There are lots of ways of fiddling around the logic of this, but this will do for this demonstration)

Plot the data!

Add contour lines highlighting some special value(s):

Add other annotations!

Now faff with gridlines – This takes an unreasonable amount of effort ☹

Add the colorbar Axes:

Finally, set the margins and make the plot!