Time Series with DateLine
Pygal offers a number of functions for working with data indexed by date, time, and datetime. DateLine is one of those options that easily plots time series data indexed by dates. This example uses sunspots data to generate a time series plot
The snippet below downloads sunspots data into a dataset folder in the current working directory
!wget -c -b http://www-personal.umich.edu/~mejn/cp/data/sunspots.txt -P dataset
Continuing in background, pid 21870. Output will be written to ‘wget-log’.
import pygal
import pandas as pd
import numpy as np
# reading and processing the sunspots data
sunspots = pd.read_csv('dataset/sunspots.txt', delimiter='\t', header=None)
sunspots.columns = ['month', 'sunspots']
sunspots['date'] = sunspots.apply(lambda x:(pd.Timestamp('1749-01-01')+ pd.DateOffset(months = int(x['month']))),axis=1)
sunspots.drop(['month'], axis=1, inplace=True )
sunspots = sunspots[['date', 'sunspots']][:20]
sunspots.head()
date | sunspots | |
---|---|---|
0 | 1749-01-01 | 58.0 |
1 | 1749-02-01 | 62.6 |
2 | 1749-03-01 | 70.0 |
3 | 1749-04-01 | 55.7 |
4 | 1749-05-01 | 85.0 |
ts_plot = pygal.DateLine(x_label_rotation=-35, title='Monthly Observed Sunspots')
ts_plot.x_labels = sunspots.date
ts_plot.add('Sunspots', list(zip(sunspots.date,sunspots.sunspots)))
ts_plot.render_in_browser()