Matplotlib 不使用 DataReader 在图表上显示日期
Matplotlib does not show dates on the chart using datareader
我对使用 Python 和尝试显示简单的股票图表还很陌生。我在使用 dateFrame 时看到过类似的问题,但在 datareader 中没有看到。
这是代码示例。问题是日期没有出现在底部。不确定我是否需要或声明它是索引。
import pandas_datareader.data as web
import datetime
import matplotlib.pyplot as plt
%matplotlib inline
start = datetime.datetime(2017, 1, 1)
end = datetime.datetime(2018, 6, 8)
stock = web.DataReader('GOOG', 'iex', start, end)
stock_name = 'Google'
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
stock['close'].plot(ax=ax,grid = True, color='blue',fontsize=14,legend=False)
ax.set_title(stock_name, fontsize=18)
ax.set_xlabel('Date',fontsize=14)
目前,您的索引是 "object" 类型。您只需要将其转换为 "datetime"。阅读 DataFrame 后尝试以下操作。
stock.index = pd.to_datetime(stock.index)
我对使用 Python 和尝试显示简单的股票图表还很陌生。我在使用 dateFrame 时看到过类似的问题,但在 datareader 中没有看到。
这是代码示例。问题是日期没有出现在底部。不确定我是否需要或声明它是索引。
import pandas_datareader.data as web
import datetime
import matplotlib.pyplot as plt
%matplotlib inline
start = datetime.datetime(2017, 1, 1)
end = datetime.datetime(2018, 6, 8)
stock = web.DataReader('GOOG', 'iex', start, end)
stock_name = 'Google'
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
stock['close'].plot(ax=ax,grid = True, color='blue',fontsize=14,legend=False)
ax.set_title(stock_name, fontsize=18)
ax.set_xlabel('Date',fontsize=14)
目前,您的索引是 "object" 类型。您只需要将其转换为 "datetime"。阅读 DataFrame 后尝试以下操作。
stock.index = pd.to_datetime(stock.index)