Python 带日期时间的Matplotlib绘图

Python 带日期时间的Matplotlib绘图,python,matplotlib,machine-learning,plot,data-visualization,Python,Matplotlib,Machine Learning,Plot,Data Visualization,我使用python中的matplotlib可视化数据,数据是股票价格历史记录(每天的数据),并且这些数据有日期时间,我想创建一个日期时间为x轴的绘图(但只在x轴上显示年份,因为它没有足够的空间显示所有数据) 我的数据集是: 我的代码是 plt.figure(figsize=(16,8)) plt.title('Stock Price History') plt.plot(df['Close']) plt.xlabel('Date', fontsize=18) plt.ylabel('Stock

我使用python中的matplotlib可视化数据,数据是股票价格历史记录(每天的数据),并且这些数据有日期时间,我想创建一个日期时间为x轴的绘图(但只在x轴上显示年份,因为它没有足够的空间显示所有数据)

我的数据集是:

我的代码是

plt.figure(figsize=(16,8))
plt.title('Stock Price History')
plt.plot(df['Close'])
plt.xlabel('Date', fontsize=18)
plt.ylabel('Stock Price', fontsize = 18)
plt.show()
但x轴的结果是线数据的索引,你们能帮我修复它吗,我真的很感谢!!
您可以为绘图设置XTICK。这将为每250个数据点设置一次

fig = plt.figure(figsize=(16,8))
plt.title('Stock Price History')
plt.plot(df['Date'], df['Close'])
plt.xlabel('Date', fontsize=18)
plt.ylabel('Stock Price', fontsize = 18)
ax = plt.axes()
ax.set_xticks(ax.get_xticks()[::250])
plt.show()

您是否尝试重置索引?也可以在plt.plot-function中调用x,而不是仅调用y。-->plt.绘图(df['date'],df['close'])