Last week we looked at how to create a simple base map with Cartopy. In this week’s MetPy Monday, we learn about contouring a field on the map and some of the idiosyncrasies of cyclic points. In the end, we will have a plot of the globe with the Coriolis parameter contoured. You can use this functionality to create height maps and more!
We’ll start off with importing the tools we will use: matplotlib, MetPy calculations, MetPy units, and numpy. We’re also using the magic %matplotlib inline so figures show up in the notebook instead of in separate windows.
Next, I create an array of latitudes from -90 to 90 degrees and then using MetPy’s calculation module to calculate the Coriolis parameter at each of these latitudes.
To verify things to ourselves, I made a quick plot of the Coriolis parameter as a function of latitude. We see the non-linear behavior we expect, with the absolute value of the parameter increasing towards the poles.
Next, I create an array of longitudes from 0-359 degrees and broadcast the Coriolis parameter we calculated into that shape. The wrinkle comes from the fact that longitudes are cyclic. We roll from 359 degrees back to 0 and start going around the globe again. The contouring algorithm isn’t equipped to understand this by default. If you just contour what we have now, there is a break at 0 degrees longitude in every contour. We can use the cartopy utility addcyclicpoint to create a cyclic coordinate system that will contour correctly. We pass the data and coordinates to the function and get back data and coordinates with a cyclic element.
Now we’re ready to contour! We go about making the base map in the now familiar way. (If you need a refresher, checkout MetPy Monday #6.) We use matplotlib’s contour method to calculate and draw the contours, drawing 20 contour lines in total. Don’t forget to specify the transform so everything plots on the map! I grab the contours as CS and set their label properties to be inline and a sensible single point after the decimal. By default, the contours of negative values are dashed. I didn’t like the look of that, so I set the contour.negative_linestyle parameter to solid.
The resulting map looks pretty good for only a few lines of code! It’s worth spending some time exploring the matplotlib documentation for contour and contourf (filled contours). There are a lot of customizations that can be done to make your map look however you wish.
My notebook is available here. Thanks for following along on another MetPy Monday!