Lesson 2. Activity: Plot Time Series Data Using Pandas in Open Source Python

Chapter Five - Practice Your Plotting Skills

In this chapter, you will practice your skills creating different types of plots in Python using earthpy, matplotlib, and folium.

Learning Objectives

  • Apply your skills in plotting time series data using matplotlib and pandas in open source Python.

Plot Time Series Data in Python

Time series data formats apply to many different types of data including precipitation, temperature, land use change data, and much more. Plotting time series data can be particularly tricky given varying time stamp formats, time zone differences and your analysis needs. In this lesson you will practice you skills associated with plotting time series data in Python. To review how to work with time series data using Pandas, check out the chapter of time series data in the intermediate earth data science textbook.

Below is you will find a challenge activity that you can use to practice your plotting skills for plot time series data using matplotlib and pandas. The packages that you will need to complete this activity are listed below.

# Import Packages
import os
from datetime import datetime

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import seaborn as sns
import pandas as pd
import earthpy as et

# Add seaborn general plot specifications
sns.set(font_scale=1.5, style="whitegrid")

Challenge 1: Plot Time-Series Data

The plot that you will create will show the global loss of glaciers from 1945 to the present using NOAA data. To make this plot, you will have to do the following:

  1. Read in the .csv using the API link: https://datahub.io/core/glacier-mass-balance/r/glacier-mass-balance_zip.zip using pandas to create a DataFrame.
  2. Parse the dates from the .csv file. Assign the date column to be a DataFrame index.
  3. Plot your data making sure datetime is on the x-axis and Mean cumulative mass balance column is on the y-axis.
  4. Set an appropriate xlabel, ylabel, and plot title.
  5. Change the x limits to range from 1940 to 2020. Use the ax.set_xlim() argument, and ensure that you create your limits as datetime objects. For example, if the lower xlimit was to be set for 1920, I would create it using datetime(1920, 1, 1) to say the datetime is for January 1st, 2020.
  6. Open and look at the metadata found in the README.md file of your download, to find out what the units for the Mean cumulative mass balance are.

The plot below is an example of what your final plot should look like after completing this challenge.

# Download the data & Set your working directory
Downloading from https://ndownloader.figshare.com/files/24649952
Extracted output to /root/earth-analytics/data/earthpy-downloads/glacier-mass-balance
Line graph showing the global glacier mass balance since 1945.
Line graph showing the global glacier mass balance since 1945.

Leave a Comment