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.

Overview

This Project Pythia Cookbook will cover creating Skew-T plots with MetPy.

In this Cookbook, you will learn:

  1. The foundations of creating a basic Skew-T with MetPy.

  2. Adding advanced features to a Skew-T such as Convective Available Potential Energy (CAPE) and Convective Inhibition (CIN) shading, Lifted condensation Level marker (LCL) and parcel profile.

  3. Creating a Hodograph with multiple layout options.

  4. Accessing remote Upper Air data with Siphon from University of Wyoming and Iowa State University.

  5. Using MetPy to calculate several Skew-T parameters.

Prerequisites

ConceptsImportanceNotes
Intermediate NumPyNecessary
Matplotlib BasicsNecessary
  • Time to learn: 30 minutes


Imports

Simple Sounding with MetPy Test Data

Use MetPy to make a simple Skew-T plot. This will serve as a base for all Skew-T plots throughout this cookbook.

Obtain Data and Format

Upper air data can be obtained using the siphon package, but for this example we will use some of MetPy’s sample data. as_file_obj=False), skiprows=5, usecols=[0, 1, 2, 3, 6, 7], names=col_names) is necessary due to the formatting of the MetPy sample data. This formatting is not needed when using upper air data obtained via Siphon. Obtaining data with Siphon will be covered in a later notebook.

Downloading file 'jan20_sounding.txt' from 'https://github.com/Unidata/MetPy/raw/v1.7.1/staticdata/jan20_sounding.txt' to '/home/runner/.cache/metpy/v1.7.1'.
Loading...

Assign Units

One of MetPy’s powerful features is its unit syntax. MetPy typically requires units to be attached to arrays before being passed to MetPy functions. The MetPy documentation has a full tutorial section on working with units.

In order to plot our sounding data, we will need to pass units to the individual variables in our dataset.

Loading...

Create Sounding Plot

<Figure size 900x900 with 1 Axes>

Let’s Unpack the the Previous Code Cell

fig = plt.figure(figsize=(6, 6))

  • This line creates the figure and sets the size. Equal proportions are typically recommended for a Skew-T.

skew = SkewT(fig, rotation=45)

  • Calls the MetPy Skew-T function to create a figure that we can easily plot upper air data on. Rotation is set at 45 to create the characteristic skewed appearence of a Skew-T.

skew.plot(pres, temp, 'red')
skew.plot(pres, dewpoint, 'green')

  • These two lines plot the temperature and dewpoint lines. The lines are set to the standard colors, but can be adjusted for colorblind accessibility. Matplotlib has extensive documentation on choosing colormaps and color vision deficiencies resources.

skew.plot_barbs(pres, u, v)

  • This line plots the wind barbs. Customization options can be found here.

skew.ax.set_xlabel('Temperature (\N{DEGREE CELSIUS})')
skew.ax.set_ylabel('Pressure (hPa)')
skew.ax.set_ylim(1000, 100)

  • These lines improve the axes labels and set the y-axis limit to the standard values typically used in a Skew-T diagram.

skew.plot_dry_adiabats()
skew.plot_moist_adiabats()
skew.plot_mixing_lines()

  • These add each relevant special line if desired. The argument colors='preferred color here' can be passed if you would like to change the colors from the default. Further customization options can be found in the MetPy documentation for each line.

fig = plt.gcf()

  • Gcf stands for get current figure. This retrieves the current figure and allows us to modify it with the following line.

add_metpy_logo(fig, 115, 100)

  • When add_metpy_logo is included in the imports cell, it allows use of the add_metpy_logo() function. This is greatly appreciated when utilizing MetPy for your plots. The position and size of the logo can be adjusted.

plt.title('Simple Sounding');

  • This adds the title to the figure. The ; removes text that populates above the figure.

Now, Let’s Improve the Wind Barbs

We will use the same code as above, but will add three additional lines of code.

<Figure size 900x900 with 1 Axes>

The wind barbs on this plot are much more readable. Let’s review the added code to understand why.

my_interval = np.arange(100, 1000, 50) * units('hPa')

  • This creates a custom spacing interval from the pressure levels the wind barbs are plotted on.

ix = mpcalc.resample_nn_1d(pres, my_interval)

  • We defined where we want the wind barbs with my_interval. Since mb is not an index in our dataset, we have to use the function mpcalc.resample_nn_1d() to determine what index value each interval closest matches up with. Further information can be found in the MetPy documentation.

skew.plot_barbs(pres[ix], u[ix], v[ix])

  • Now we just add the ix variable to each parameter in .plot_barbs() to produce the wind barbs at the interval we set.

Optional Step

We can extend the moist adiabat, dry adiabat, and mixing ratio lines throughout the figure by adding including optional parameters. See the documentation for details on how to further adjust these for readability preferences.

/home/runner/micromamba/envs/metpy-cookbook/lib/python3.14/site-packages/scipy/integrate/_ivp/base.py:24: UserWarning: Saturation mixing ratio is undefined for some requested pressure/temperature combinations. Total pressure must be greater than the water vapor saturation pressure for liquid water to be in equilibrium.
  return np.asarray(fun(t, y), dtype=dtype)
<Figure size 900x900 with 1 Axes>