简体   繁体   English

Matplotlib 绘制日期时间

[英]Matplotlib Plot with date time

I visualzie data with matplotlib in python, data is stock price history (data day by day) and this data have date time, i want to create a plot with date time is x axis (but only show year on x axis, because it is not space enough to show all).我在 python 中使用 matplotlib 可视化数据,数据是股票价格历史(每天的数据)并且这些数据有日期时间,我想创建一个日期时间是 x 轴的图(但只在 x 轴上显示年份,因为它是没有足够的空间来显示所有)。

My dataset is: https://drive.google.com/file/d/1E1KKXxdIthSRG9aN6QJBetyy1i5_Kjie/view?usp=sharing我的数据集是: https : //drive.google.com/file/d/1E1KKXxdIthSRG9aN6QJBetyy1i5_Kjie/view?usp=sharing

My code is我的代码是

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()

but result in x axis is index of line data, can you help me fix it, i really thank you!!但是x轴的结果是线数据的索引,你能帮我修复它吗,我真的很感谢你!! 在此处输入图片说明

You can set xticks for your plot.您可以为您的情节设置 xticks。 This sets it for every 250th data point.这为每 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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM