from datetime import datetime
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
from metpy.plots import SkewT, Hodograph
from metpy.units import pandas_dataframe_to_unit_arrays, units
import numpy as np
from siphon.simplewebservice.wyoming import WyomingUpperAir
from metpy.calc import wind_speed
The first step in plotting a skew-T is accessing the sounding data. We're going to download some observed sounding data from the University of Wyoming sounding archive from 00z on 15 April 2015 at Topeka, KS during a major tornado outbreak. First, we create a datetime object for MetPy to use to select the correct time and station from the Wyoming archive.
dt = datetime(2012, 4, 15, 0)
station = 'TOP'
Next, we'll download the data from the Wyoming archive into a Pandas dataframe with units. You'll need an internet connection for this step.
df = WyomingUpperAir.request_data(dt, station)
data = pandas_dataframe_to_unit_arrays(df)
Now we'll split that Pandas dataframe up into individual arrays for each variable with units, and calculate wind speed from the wind components.
p = data['pressure']
T = data['temperature']
Td = data['dewpoint']
u = data['u_wind']
v = data['v_wind']
spd = wind_speed(u,v)
Now we'll plot the data we downloaded on a skew-T diagram.
First, we'll create a figure using Matplotlib and modify it into a skew-T plot using MetPy's SkewT class.
fig = plt.figure(figsize=(9, 11))
skew = SkewT(fig, rotation=45)
Next, we'll add code to plot temperature and dewpoint data as red and green lines on the Skew-T, and add the wind data as barbs along the right side of the plot.
skew.plot(p, T, 'r')
skew.plot(p, Td, 'g')
skew.plot_barbs(p[::3], u[::3], v[::3], y_clip_radius=0.03)
fig
We can set the limits of the plot using set_xlim and set_ylim.
skew.ax.set_xlim(-30, 40)
skew.ax.set_ylim(1020, 100)
fig
We can also add dry adiabats, moist adiabats, and mixing ratio lines with the next three lines of code.
skew.plot_dry_adiabats(alpha=0.25, color='orangered')
skew.plot_moist_adiabats(alpha=0.25, color='tab:green')
skew.plot_mixing_lines(linestyle='dotted', color='tab:blue')
fig
Finally, let's add a title to the plot
skew.ax.set_title('{} Sounding'.format(station), loc='left')
skew.ax.set_title('Valid Time: {}'.format(dt), loc='right')
fig
In this section, we'll add a hodograph inset to the plot. The first section of code here is the same as the previous skew-T plot. The next two lines add a hodograph inset using matplotlib's inset_axes functionality and MetPy's Hodograph class.
fig = plt.figure(figsize=(9, 11))
skew = SkewT(fig, rotation=45)
skew.plot(p, T, 'r')
skew.plot(p, Td, 'g')
skew.plot_barbs(p[::3], u[::3], v[::3], y_clip_radius=0.03)
skew.ax.set_xlim(-30, 40)
skew.ax.set_ylim(1020, 100)
skew.plot_dry_adiabats(alpha=0.25, color='orangered')
skew.plot_moist_adiabats(alpha=0.25, color='tab:green')
skew.plot_mixing_lines(linestyle='dotted', color='tab:blue')
skew.ax.set_title('{} Sounding'.format(station), loc='left')
skew.ax.set_title('Valid Time: {}'.format(dt), loc='right')
#Add hodograph inset using matplotlib and MetPy
ax_hod = inset_axes(skew.ax, '30%', '30%', loc=1)
h = Hodograph(ax_hod, component_range=80)
Next, we'll add a grid to the hodograph.
h.add_grid(increment=20)
fig
Finally, we'll plot the wind data below 150 hPa on the hodograph and color it by speed.
h.plot_colormapped(u[p>150*units('hPa')], v[p>150*units('hPa')], spd[p>150*units('hPa')])
fig