import numpy as np import matplotlib.pyplot as plt import matplotlib.cbook as cbook import matplotlib.ticker as ticker
# Load a numpy record array from yahoo csv data with fields date, open, close, # volume, adj_close from the mpl-data/example directory. The record array # stores the date as an np.datetime64 with a day unit ('D') in the date column. with cbook.get_sample_data('goog.npz') as datafile: r = np.load(datafile)['price_data'].view(np.recarray) r = r[-30:] # get the last 30 days # Matplotlib works better with datetime.datetime than np.datetime64, but the # latter is more portable. date = r.date.astype('O')
# first we'll do it the default way, with gaps on weekends fig, axes = plt.subplots(ncols=2, figsize=(8, 4)) ax = axes[0] ax.plot(date, r.adj_close, 'o-') ax.set_title("Default") fig.autofmt_xdate()
# next we'll write a custom formatter N = len(r) ind = np.arange(N) # the evenly spaced plot indices