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


Practice Your Python Plotting Skills - Scientists guide to plotting data in python textbook course module

Welcome to the first lesson in the Practice Your Python Plotting Skills module. This chapter provides a series of activities that allow you to practice your Python plotting skills using differen types of data.

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.

Challenge: Plot Time Series Data Using Open Source Python

One of the most common data plot the chapters have covered is time series data. Below is a challenge to refresh your memory on how to plot time series and modify certain aspects of it.

# 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.

Data Tip: For help with this challenge, see your previous activities involving time series, or the time series chapters from the EarthLab website.

# Download the data & Set your working directory
et.data.get_data(
    url="https://datahub.io/core/glacier-mass-balance/r/glacier-mass-balance_zip.zip")
os.chdir(os.path.join(et.io.HOME, "earth-analytics", "data"))
Downloading from https://datahub.io/core/glacier-mass-balance/r/glacier-mass-balance_zip.zip
Extracted output to /root/earth-analytics/data/earthpy-downloads/glacier-mass-balance_zip

Leave a Comment