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:
- Read in the
.csvusing the API link:
https://datahub.io/core/glacier-mass-balance/r/glacier-mass-balance_zip.zipusing pandas to create a
- Parse the dates from the
.csvfile. Assign the date column to be a
- Plot your data making sure datetime is on the x-axis and
Mean cumulative mass balancecolumn is on the y-axis.
- Set an appropriate xlabel, ylabel, and plot title.
- 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.
- Open and look at the metadata found in the
README.mdfile of your download, to find out what the units for the
Mean cumulative mass balanceare.
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