首页 > 解决方案 > 从 csv 文件中绘制股票数据,未正确显示日期?

问题描述

希望有人能指出我正确的方向。

我是 Matplotlib 的新手。我有一个 .csv 的股票数据,看起来像这样......

库存数据

...我想绘制开盘价,日期为 X 标签。这就是我现在正在使用的:

stock_prices = pd.read_csv(cache_filename)

# Plot the open prices
stock_prices['1. open'].plot()
plt.title('Daily Time Series for the stock (from saved CSV file)')
plt.xlabel('day')
plt.ylabel('price')
plt.show()

...但是 X 轴标记为 Day,但它只显示数字计数,而不是日期值:

立即绘制

我需要添加什么以确保 X 轴标有日期?或者指点我资源。非常感谢!

标签: pythonpython-3.xmatplotlib

解决方案


尝试

import matplotlib.pyplot as plt
plt.plot(stock_prices['date'],stock_prices['1. open'])
plt.title('Daily Time Series for the stock (from saved CSV file)')
plt.xlabel('day')
plt.ylabel('price')

推荐阅读