{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\"Matplotlib
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# LAB EXERCISE: Plot temperature data from the NYS Mesonet" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "## Overview\n", "You will replicate the workflow in a portion of the 01_MatplotlibIntro notebook, but use recent NYSM temperature data.\n", "\n", "1. Create a basic line plot.\n", "1. Add labels and grid lines to the plot.\n", "1. Plot multiple time series of data." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

TASK 1:

\n", " In the code cell below, add a line that imports the matplotlib library's `pyplot` interface with its standard alias.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from datetime import datetime" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## Read in the most recent hour's worth of NYSM observations using `pandas`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# First define the format and then define the lambda function\n", "timeFormat = \"%Y-%m-%d %H:%M:%S UTC\"\n", "# This function will iterate over each string in a 1-d array\n", "# and use Pandas' implementation of strptime to convert the string into a datetime object.\n", "parseTime = lambda x: datetime.strptime(x, timeFormat)\n", "df = pd.read_csv('/data1/nysm/latest.csv',parse_dates=['time'], date_parser=parseTime).set_index('time') " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot some temperature data:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "site1 = 'RUSH'\n", "site2 = 'BEAC'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Read in 2 meter temperature for these sites." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "site1_t2m = df.query('station == @site1')['temp_2m [degC]']\n", "site2_t2m = df.query('station == @site2')['temp_2m [degC]']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "site1_t2m" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "times = site1_t2m.index\n", "temps = site1_t2m" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "times" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

TASK 2:

\n", " Choose your own two NYSM sites and repeat the execution of the above four code cells.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Line plots\n", "\n", "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)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

TASK 3:

\n", " 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.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adding axes labels" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, add x- and y-axis labels to our `Axes` object." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

TASK 4:

\n", " 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.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

TASK 5:

\n", " 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.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adding labels and a grid" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

TASK 6:

\n", " Insert a code cell and plot 2 meter temperatures for both NYSM sites on the same plot. Include a legend and gridlines.\n", "
" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 August 2022 Environment", "language": "python", "name": "aug22" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.5" } }, "nbformat": 4, "nbformat_minor": 4 }