根据efiring的说法,matplotlib不支持NumPy
datetime64对象(至少尚不支持)。因此,转换
x为Python datetime.datetime对象:
x = x.astype(DT.datetime)
接下来,您可以指定x轴刻度线格式器,如下所示:
xfmt = mdates.DateFormatter('%b %d')ax.xaxis.set_major_formatter(xfmt)
import matplotlib.pyplot as pltimport matplotlib.dates as mdatesimport datetime as DTimport numpy as npx = np.array([DT.datetime(2013, 9, i).strftime("%Y-%m-%d") for i in range(1,5)], dtype='datetime64')x = x.astype(DT.datetime)y = np.array([1,-1,7,-3])fig, ax = plt.subplots()ax.plot(x, y)ax.axhline(linewidth=4, color='r')xfmt = mdates.DateFormatter('%b %d')ax.xaxis.set_major_formatter(xfmt)plt.show()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)