In [67]: plot(r.date.astype('O'), r.close) Out[67]: [<matplotlib.lines.Line2D object at 0x92a6b6c>]
你会看到x刻度标签都被压扁了。
1 2 3 4 5 6 7 8 9 10 11 12 13
import matplotlib.cbook as cbook import matplotlib.dates as mdates import numpy as np import matplotlib.pyplot as plt
with cbook.get_sample_data('goog.npz') as datafile: r = np.load(datafile)['price_data'].view(np.recarray)
# Matplotlib prefers datetime instead of np.datetime64. date = r.date.astype('O') fig, ax = plt.subplots() ax.plot(date, r.close) ax.set_title('Default date handling can cause overlapping labels')
# rotate and align the tick labels so they look better fig.autofmt_xdate()
# use a more precise date string for the x axis locations in the # toolbar ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d') ax.set_title('fig.autofmt_xdate fixes the labels')