The file river-level.csv
lists the height in meters above sea level of Chitterne Brook, a small river in Wiltshire, England. Heights are given as minimum, average, and maximum values for each day between 1 January 2014 and 31 December 2016.
The following code reads in the data and plots the daily river height along with its monthly average, minimum and maximum values.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('river-level.csv', index_col=0, comment='#', parse_dates=True)
rs_monthly = df.resample('M')
df['avg_level'].plot(label='Daily average')
rs_monthly['avg_level'].mean().plot(label='Monthly average')
rs_monthly['min_level'].min().plot(label='Monthly minimum')
rs_monthly['max_level'].max().plot(label='Monthly maximum')
plt.xlabel('Date')
plt.ylabel('River level /m')
plt.gca().legend()
plt.show()
Note that we need to set parse_dates=True
to force pandas to interpret the first column as a DatetimeIndex
.
The resulting plot is shown below.