Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab 8: Plotting Vector Winds with Cartopy and Gridspec

In this notebook, you’ll work with two new approaches to plotting remote sensing data in Python. In the first part of the lab, you’ll explore the cartopy package for plotting georeferenced data on the global (or subsets of the globe). Next, you’ll work with the gridspec module to organize a collection of subplots into a nicely presentable figure.

This notebook has three parts:

  1. Download vector wind data using NASA’s EarthAccess tools

  2. Explore the cartopy package and generate plots of wind in a regional domain

  3. Test plotting figures with gridspec module and create a nice figure using a collection of subplots

Import the modules for this notebook

Part 1: Downloading Vector Wind Data via NASA Earthaccess

In this lab, we will use vector wind data in the L4 Cross-Calibrated Multi-Platform (CCMP) product. Like the previous lab, we’ll access this data using a set of tools that NASA provides for gathering data available via Earthdata.

As before, start by defining a folder where the data will be downloaded:

Next, let’s “log in” to the Earthdata. This step will require your Earthdata credentials - if you set up the .netrc file as described in the [Registering for Account][../getting_started/register_accounts.md] page, you won’t need to do anything in the following cell:

Up next, we’ll provide a query to determine which files exist. Here, let’s gather all of the vector wind files for the year 2025. The query using the earthaccess module takes the following form:

Found 365 granules.

Ssing our query, we’ll download the data using the download command:

Loading...
Loading...
Loading...
Downloaded 365 files.

Part 2: Plotting with Cartopy

When working with geophysical data, we often want to plot data on a map. We can use the native coordinates of our data to make simple 2D maps, but what if we want to work with different projections and visualizations for our data? Python’s cartopy package provides a convenient set of tools to plot data on the map.

Coastlines

The cartopy package stores a copy of the global coastlines which can be plotted on a map with any given projection. The code below shows the coastline on the Robinson projection:

<Figure size 800x500 with 1 Axes>

Projections

As you can see above, the axes object provides the projection system for the map. The cartopy package has a variety of different projections for plotting mapped data. Test some of the common projections in the figure above:

Projection CodeDefault Parameters
PlateCarree()central_longitude=0.0
Mollweide()central_longitude=0.0
Orthographic()central_longitude=0.0, central_latitude=0.0
Robinson()central_longitude=0.0
InterruptedGoodeHomolosine()central_longitude=0.0

When you find your favorite projection, try changing the default central longitude/latitude to see how the plot changes.

Basemaps

Often, we may want to include a basemap image to cover parts of the map where we do not have data. cartopy has default built-in image to do just that:

<Figure size 800x500 with 1 Axes>

Basemaps from Google Earth can also be downloaded and plotted:

<Figure size 800x500 with 1 Axes>

Plotting Data on the Projected Map

Now that we have a map in our prefered projection, next we’d like to be able to plot our own data on the map. Begin by reading in one of the vector wind files we download above:

Below, we’ll plot this data on the globe. However, first let’s define our own custom colormap to approximate the visualization on the popular weather app Windy.com:

With our data and wind colormap in hand, we’re ready to plot the data. The cartopy plotting uses the same structure as typical plots - the only difference is that the projection information needs to be provided at each relevant location. Take a look at the following example:

<Figure size 800x500 with 2 Axes>

Creating a regional map in a projected coordinate system

Just as with regular 2D plots, we can plot just a subset of the data using the extent list. Just like the plotting of the data on the map, however, we will also need to projection information. We can modify the plot above be just in a subset around Central America by defining an extent with longitude in the range -125 to -75, and latitude in the range 0 to 30.

<Figure size 800x500 with 2 Axes>

Rotating a Plot

Often, we want our maps to be focused on our particular region of the global ocean. For example, we may want to center our plot on the Pacific Ocean when this is the focus of our study area. To do this, we can use the central_longitude and central_latitude keywords in the projections. Let’s check this out with the Orthographic projection:

<Figure size 640x480 with 2 Axes>

Adding Longitude and Latitude Lines

Often a coordinate system is advantageous on projected maps to provide a geographic context. Longitude and latitude lines can be added with the help of the matplotlib.ticker module. Here is an example with the Orthographic projection:

<Figure size 640x480 with 2 Axes>

🤔 Try it for yourself

Using the code above as an example data set, generate a similar plot in a region which fits your interests. Test out different projections and colorbars to get familiar with cartopy and create a nice figure for your region.

Part 3: Plotting Figures with Multiple Panels

When comparing data, we often want to look at different snapshots in time on the same figure. In this notebook, we will investigate matplotlib’s gridspec module to arrange subplots on the same figure.

Plots with 4 Panels

In the plot below, we will investigate snapshots of global wind speeds in 4 distinct months: January, April, July, and October. The Gridspec module allows us to control the layout of a figure with multiple panels. Begin identifying 4 files in the months listed above. Then, read in the grid for each file.

Using the files above, read in the data:

Next, use GridSpec to arrange the plots in a 2 x 2 grid:

<Figure size 1000x700 with 5 Axes>

As you can see, each subplot is added into the gs object. In addition, the projection information is provided directly into the axis object.

Generating the Average Wind Speed Fields

In the plot above, we visualize snapshots of wind in a given period. As winds change day to day, investigating means over time (rather than a single snap shot) can give us a sense of longer-term changes through time.

An averaging function

In the previous classes, we’ve constructed functions to make timeseries. Here, let’s define a function that can compute the averages of a set of files given their file paths:

Next, use the function to compute averages in January, April, July, and October

Finally, plot the monthly averages in the maps below (same code as above, but updated to averages):

<Figure size 1000x700 with 5 Axes>

🤔 Putting it all together

The above examples show how to plot data geographically and organize plots in subpanels of a figure. And in the previous labs, we generated time series from a collection of files. Let’s put all of these ideas together. In your region of interest, generate a figures which has 3-4 projected map panels showing wind data through time. Then, in the same figure, make a time series which show the wind speed in your region of interest. Be sure to provide indications in your time series (with annotations) where you map panels fall along your time series.