Matplotlib Logo

LAB EXERCISE: Plot temperature data from the NYS Mesonet


Overview

You will replicate the workflow in a portion of the 01_MatplotlibIntro notebook, but use recent NYSM temperature data.

  1. Create a basic line plot.

  2. Add labels and grid lines to the plot.

  3. Plot multiple time series of data.


Imports

Let’s import the matplotlib library’s pyplot interface; this interface is the simplest way to create new Matplotlib figures. To shorten this long name, we import it as plt to keep things short but clear. We also import the pandas library, using its standard alias of pd. Finally, we import the datetime library, which allows for efficient operations on time-based variables and datasets.

TASK 1:

In the code cell below, add a line that imports the matplotlib library’s pyplot interface with its standard alias.

import pandas as pd
from datetime import datetime

Read in the most recent hour’s worth of NYSM observations using pandas.

# First define the format and then define the lambda function
timeFormat = "%Y-%m-%d %H:%M:%S UTC"
# This function will iterate over each string in a 1-d array
# and use Pandas' implementation of strptime to convert the string into a datetime object.
parseTime = lambda x: datetime.strptime(x, timeFormat)
df = pd.read_csv('/data1/nysm/latest.csv',parse_dates=['time'], date_parser=parseTime).set_index('time')   
df
station temp_2m [degC] temp_9m [degC] relative_humidity [percent] precip_incremental [mm] precip_local [mm] precip_max_intensity [mm/min] avg_wind_speed_prop [m/s] max_wind_speed_prop [m/s] wind_speed_stddev_prop [m/s] ... snow_depth [cm] frozen_soil_05cm [bit] frozen_soil_25cm [bit] frozen_soil_50cm [bit] soil_temp_05cm [degC] soil_temp_25cm [degC] soil_temp_50cm [degC] soil_moisture_05cm [m^3/m^3] soil_moisture_25cm [m^3/m^3] soil_moisture_50cm [m^3/m^3]
time
2023-02-16 18:25:00 ADDI 7.8 7.7 68.0 0.0 0.0 0.0 2.0 2.6 0.3 ... -1.0 0.0 0.0 0.0 3.4 2.9 2.9 0.53 0.44 0.43
2023-02-16 18:30:00 ADDI 7.8 7.7 68.9 0.0 0.0 0.0 1.5 2.1 0.2 ... -1.0 0.0 0.0 0.0 3.5 2.9 2.9 0.53 0.44 0.43
2023-02-16 18:35:00 ADDI 8.1 8.0 68.3 0.0 0.0 0.0 0.9 1.5 0.3 ... -1.0 0.0 0.0 0.0 3.5 2.9 2.9 0.53 0.44 0.43
2023-02-16 18:40:00 ADDI 8.7 8.4 66.5 0.0 0.0 0.0 0.9 1.8 0.5 ... -1.0 0.0 0.0 0.0 3.5 2.9 2.9 0.53 0.44 0.43
2023-02-16 18:45:00 ADDI 8.9 8.5 64.9 0.0 0.0 0.0 1.6 2.1 0.2 ... -1.0 0.0 0.0 0.0 3.5 2.9 2.9 0.53 0.44 0.43
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2023-02-16 19:05:00 YORK 7.2 6.5 69.2 0.0 0.0 0.0 3.5 5.5 0.7 ... 0.0 0.0 0.0 0.0 5.8 4.6 4.1 0.25 0.28 0.34
2023-02-16 19:10:00 YORK 7.0 6.2 70.1 0.0 0.0 0.0 3.2 5.2 0.9 ... 0.0 0.0 0.0 0.0 5.8 4.6 4.1 0.25 0.28 0.34
2023-02-16 19:15:00 YORK 6.7 6.1 70.5 0.0 0.0 0.0 2.8 4.5 0.7 ... 0.0 0.0 0.0 0.0 5.8 4.6 4.1 0.25 0.28 0.34
2023-02-16 19:20:00 YORK 6.5 5.9 70.7 0.0 0.0 0.0 3.4 5.1 0.8 ... 0.0 0.0 0.0 0.0 5.8 4.6 4.1 0.25 0.28 0.34
2023-02-16 19:25:00 YORK 6.4 5.8 71.8 0.0 0.0 0.0 3.4 5.0 0.8 ... 0.0 0.0 0.0 0.0 5.8 4.6 4.1 0.25 0.28 0.34

1638 rows × 29 columns

Plot some temperature data:

Instead of “hard-coding” lists of variables and hours, Pandas creates “list-like” objects, called Series. First, let’s specify a couple of NYSM sites, and then retrieve time and temperature data from the data file.

site1 = 'RUSH'
site2 = 'BEAC'

Read in 2 meter temperature for these sites.

site1_t2m = df.query('station == @site1')['temp_2m [degC]']
site2_t2m = df.query('station == @site2')['temp_2m [degC]']
site1_t2m
time
2023-02-16 18:25:00    5.0
2023-02-16 18:30:00    4.8
2023-02-16 18:35:00    4.7
2023-02-16 18:40:00    4.7
2023-02-16 18:45:00    4.6
2023-02-16 18:50:00    4.6
2023-02-16 18:55:00    4.4
2023-02-16 19:00:00    4.6
2023-02-16 19:05:00    4.5
2023-02-16 19:10:00    4.5
2023-02-16 19:15:00    4.2
2023-02-16 19:20:00    4.1
2023-02-16 19:25:00    4.1
Name: temp_2m [degC], dtype: float64
times = site1_t2m.index
temps = site1_t2m
times
DatetimeIndex(['2023-02-16 18:25:00', '2023-02-16 18:30:00',
               '2023-02-16 18:35:00', '2023-02-16 18:40:00',
               '2023-02-16 18:45:00', '2023-02-16 18:50:00',
               '2023-02-16 18:55:00', '2023-02-16 19:00:00',
               '2023-02-16 19:05:00', '2023-02-16 19:10:00',
               '2023-02-16 19:15:00', '2023-02-16 19:20:00',
               '2023-02-16 19:25:00'],
              dtype='datetime64[ns]', name='time', freq=None)

TASK 2:

Choose your own two NYSM sites and repeat the execution of the above four code cells.

Line plots

Let’s create a Figure whose dimensions, if printed out on hardcopy, would be 10 inches wide and 6 inches long (assuming a landscape orientation). We then create an Axes, consisting of a single subplot, on the Figure. After that, we call plot, with hours as the data along the x-axis (independent values) and temps as the data along the y-axis (the dependent values).

TASK 3:

Insert a code cell and use Matplotlib to create a time series plot of 2m temperature versus time for the first NYSM site you chose.

Adding axes labels

Next, add x- and y-axis labels to our Axes object.

TASK 4:

Insert a code cell and add x- and y-axis labels to your Axes object you just created. Also, add a meaningful title with an appropriately-readable font size.

TASK 5:

Insert a code cell and add x- and y-axis labels to your Axes object you just created. Also, add a meaningful title with an appropriately-readable font size.

Adding labels and a grid

Here we call plot more than once to plot multiple series of temperature on the same plot; when plotting we pass label to plot to facilitate automatic creation of legend labels. This is added with the legend call. We also add gridlines to the plot using the grid() call.

TASK 6:

Insert a code cell and plot 2 meter temperatures for both NYSM sites on the same plot. Include a legend and gridlines.