Search code examples
pythonpandasmatplotlibpandas-datareader

Matplotlib for google stock price example in python data science handbook


Using the Python data science handbook (pg.198 Fig 3.6 resampling and converting frequencies for anyone from google), I'm trying to follow the example, which is as below:

%matplotlib inline
import pandas as pd
import numpy as np
from pandas_datareader import data
import matplotlib.pyplot as plt
import seaborn; seaborn.set()


goog = data.DataReader('GOOG', start='2004', end='2016',
                       data_source='google')

goog = goog['Close']

goog.plot(alpha=0.5, style='-')
goog.resample('BA').mean().plot(style=':')
goog.asfreq('BA').plot(style='--');

plt.legend(['input', 'resample', 'asfreq'],
           loc='upper left');

My graph looks like this: this

While the example look like this:

book version

Why is this the case? I'm pretty certain that the code is an exact duplicate. How can I solve this issue?


Solution

  • There is nothing really wrong, just that the resampling rule BA with mean() returns only two points, thus the straight line.

    Playing with the different available offset rules I don't manage to quickly reproduce the desired plot. You should try to explore and use perhaps a multiple of days.