python - Matplotlib 绘制日期时间图

标签 python matplotlib machine-learning plot data-visualization

我在python中使用matplotlib可视化数据,数据是股票价格历史记录(逐日数据)并且该数据具有日期时间,我想创建一个日期时间为x轴的图(但仅在x轴上显示年份,因为没有足够的空间来显示全部)。

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

我的代码是

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轴的结果是行数据的索引,你能帮我解决这个问题吗,我真的很感谢!! enter image description here

最佳答案

您可以为您的绘图设置 xticks。这会为每 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()

enter image description here

关于python - Matplotlib 绘制日期时间图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60124644/

相关文章:

python - Django:如何创建多选表单?

python - 如何重新缩放图像坐标的信息以在相似图像的缩放版本上工作

python - 将 SHAP 汇总图另存为 PDF/SVG

python - 使用 matplotlib.colors.Colormap 将标量值映射到 RGB 时出现奇怪的颜色图

python - 将验证数据传递到 Keras Sequential 中的 .fit 时,生成器未被识别

machine-learning - 从真实值和预测值获取准确性

python - 带有 reStructuredText 的项目符号列表中的源代码

python - 如何让 PyC​​harm 呈现我创建的图表而不是绘图对象?

python - 为 3D 形状着色

python - 如何在Python中绘制簇?