import numpy as np import matplotlib.pyplot as plt import matplotlib.dates as mdates import matplotlib.cbook as cbook years = mdates.YearLocator() # every year months = mdates.MonthLocator() # every month years_fmt = mdates.DateFormatter('%Y') # Load a numpy structured array from yahoo csv data with fields date, open, # close, volume, adj_close from the mpl-data/example directory. This array # stores the date as an np.datetime64 with a day unit ('D') in the 'date' # column. with cbook.get_sample_data('/home/jens/Downloads/goog.npz') as datafile: data = np.load(datafile)['price_data'] print (data) fig, ax = plt.subplots() ax.plot('date', 'adj_close', data=data) # format the ticks ax.xaxis.set_major_locator(years) ax.xaxis.set_major_formatter(years_fmt) ax.xaxis.set_minor_locator(months) # round to nearest years. datemin = np.datetime64(data['date'][0], 'Y') datemax = np.datetime64(data['date'][-1], 'Y') + np.timedelta64(1, 'Y') ax.set_xlim(datemin, datemax) # format the coords message box ax.format_xdata = mdates.DateFormatter('%Y-%m-%d') ax.format_ydata = lambda y: '$%1.2f' % y # format the price. ax.grid(True) # rotates and right aligns the x labels, and moves the bottom of the # axes up to make room for them fig.autofmt_xdate() plt.show()