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 EXERCISE: Plot temperature data from the NYS Mesonet

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; after creating the Figure and Axes with it, we will use Matplotlib’s object-oriented interface for the further construction of the figure. 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.

import pandas as pd
from datetime import datetime
import matplotlib.pyplot as plt

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

# First define how the date and time are formatted in the CSV file
date_format = "%Y-%m-%d %H:%M:%S UTC"
# Read in the CSV file
df = pd.read_csv('/data1/nysm/latest.csv',parse_dates=['time'], date_format = date_format).set_index('time')   
# Displays all rows within the dataframe (comment this out to only show first and last few rows):
#pd.set_option('display.max_rows', None)
df
Loading...

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 (Rush, and Beacon, NY), 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]']
times = site1_t2m.index
temps = site1_t2m

TASK 1:

In the cells below, display (print) text output for times and site1_t2m.
#Insert code here:
#Insert code here:

TASK 2:

Choose your own two NYSM sites (site3_t2m and site4_t2m) and repeat the execution of the above code cells.
Hint: The station IDs can be found here: https://www.nysmesonet.org/about/sites
#Insert code here:

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 times 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.
#Insert code here:

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.
#Insert code here:

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 5:

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

Try to plot another variable

TASK 6:

Next, insert code cells to import data from two different stations, using a different variable (be sure to enter the exact text corresponding to the variable you are interested in importing, at the top of each column in the dataframe). Plot the data using different colors and/or line types.
#Insert code here: