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.

Matplotlib

Authors
Affiliations
University at Albany (SUNY)
Univerity of California, Berkeley
University of Miami
University of California, San Diego
University at Albany (SUNY)
Tuskegee University
University at Albany (SUNY)

Overview

In this notebook, we utilize matplotlib contour functions to detect regions in gridded climate data with low precipitation values based on a defined threshold. We label these areas as individual features and follow them through various time steps. We visualize these regions across time on a map using frame-to-frame overlap, allowing us to analyze how the precipitation systems move and evolve over time.

Prerequisites

ConceptsImportanceNotes
XarrayNecessaryFor reading in data
MatplotlibNecessaryFor contouring
CartopyUsefulFor map projection
IPythonUsefulFor inline animation

Estimated time to learn: 30 minutes

Import Packages

Load in the data using xarray, and extract the variable of interest.

We want to look at the total precipitation of the extra-tropical cyclones, which is stored under the ETC_tp variable.

Exploratory Data Analysis

Let’s take a look at this precipitation data for a single time stamp.

<Figure size 640x480 with 1 Axes>

The majority of precipitation values are between 0 and 2 mm. Let’s use that as a filter so we can visualize the data in a way that corresponds to the bulk of the data, rather than a few askew values.

/home/runner/micromamba/envs/feature-tracking-dev/lib/python3.14/site-packages/cartopy/io/__init__.py:263: DownloadWarning: Downloading: https://naturalearth.s3.amazonaws.com/110m_physical/ne_110m_coastline.zip
  warnings.warn(f'Downloading: {url}', DownloadWarning)
<Figure size 1200x400 with 2 Axes>

Track Features Over Time

We will find the contours later using matplotlib.pyplot.contour.

First, we initialize the contour-detection parameters, and extract the latitude and longitude coordinates from the dataset. We then convert the coordinates into 2D grids so the found contours can be plotted in geographic space.

The feature we want to detect is connected regions enclosed by a precipitation contour at or below our defined threshold.

We create a function to detect these features at each time step and assign each feature a track ID (track_id) so we can follow it from one time step to the next.

  1. Detect features in the current field by drawing a contour at the precipitation threshold.

    • Each contour line is converted into a closed region (mask).

    • Only regions where precipitation is below the threshold are kept.

    • Very small regions (below min_area) are removed.

  2. For each detected feature, compare it with features from the previous time step.

    • Compute spatial overlap between the current feature mask and previous feature masks. If the new feature overlaps enough (exceeds min_overlap) with an old feature, it keeps the same track_id. Otherwise, it is treated as a new feature and receives a new track_id.

  3. Store the feature’s track_id, centroid location, and mask for plotting or later analysis.

Note that our function identifies these features for a single time step. So to track these regions over time, we must iterate through all time steps present in our data, and call detect_features on each interval.

Run the tracking function for each time step, and save the results in a variable tracked.

In the visualization, we will have a colorbar that represent precipitation levels. We want the bar to accurately depict the bulk of the data, so we limit the lower end to the 1st percentile of the data, and the upper end to the 99th percentile.

We can animate the storm tracked over time by plotting the low precipitation regions at each timestep.

Loading...

This animation showcases the total precipitation values of low precipitation systems from hours 0 to 12. These feature systems appear between the latitudes 30°N and 70°N (hence the boundaries of the map) which is typical for an extratropical cyclone.

Through tracking, we see that most of the systems head towards the East as the hour increases. We also are able to track whether these precipitation areas break apart, which is typical behavior during an extratropical cyclone. For example, see the leftmost precipitation system on the map. It heads toward the East direction and splits at hour 7.