{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\"Matplotlib
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# COMPLETED 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 matplotlib.pyplot as plt\n", "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": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "site3 = 'MANH'\n", "site4 = 'TANN'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "site3_t2m = df.query('station == @site3')['temp_2m [degC]']\n", "site4_t2m = df.query('station == @site4')['temp_2m [degC]']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "site3_t2m" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "times" ] }, { "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 `times` 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": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = plt.figure(figsize=(10, 6))\n", "ax = fig.add_subplot(1, 1, 1)\n", "\n", "ax.plot(times, site3_t2m)" ] }, { "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": [ "### Adding labels and a title" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Add labels and title\n", "ax.set_xlabel('Date/Hour (EST)')\n", "ax.set_ylabel('2 m Temperature (°C)')\n", "ax.set_title(f'Hourly Data for {site3}')\n", "\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plot multiple sites" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

TASK 5:

\n", " Insert a code cell and plot 2 meter temperatures for both NYSM sites on the same plot. Include a legend and gridlines.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = plt.figure(figsize=(10, 6))\n", "ax = fig.add_subplot(1, 1, 1)\n", "\n", "# Plot two series of data\n", "# The label argument is used when generating a legend.\n", "ax.plot(times, site3_t2m, label=site3)\n", "ax.plot(times, site4_t2m, label=site4)\n", "\n", "# Add labels and title\n", "ax.set_xlabel('Date/Hour (EST)')\n", "ax.set_ylabel('Temperature (°C)')\n", "ax.set_title('Hourly Data for selected NYSM Sites')\n", "\n", "# Add gridlines\n", "ax.grid(True)\n", "\n", "# Add a legend to the upper left corner of the plot\n", "ax.legend(loc='upper left')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Try to plot another variable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

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.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "variable2 = 'relative_humidity [percent]'\n", "site5 = 'BEAC'\n", "site6 = 'OLEA'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "data5 = df.query('station == @site5')[variable2]\n", "data6 = df.query('station == @site6')[variable2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = plt.figure(figsize=(10, 6))\n", "ax = fig.add_subplot(1, 1, 1)\n", "\n", "# Plot two series of data\n", "# The label argument is used when generating a legend.\n", "ax.plot(times, data5, label=site5, color='tab:green', linestyle='-')\n", "ax.plot(times, data6, label=site6, color='indigo', linestyle=':')\n", "\n", "# Add labels and title\n", "ax.set_xlabel(\"Date/time (EST)\")\n", "ax.set_ylabel('Relative Humidity (%)')\n", "ax.set_title('Hourly Data for selected NYSM Sites')\n", "\n", "# Add gridlines\n", "ax.grid(True)\n", "\n", "# Add a legend to the upper left corner of the plot\n", "ax.legend(loc='upper left');" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 Jan. 2024 Environment", "language": "python", "name": "jan24" }, "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.11.7" } }, "nbformat": 4, "nbformat_minor": 4 }