问题描述
我正在尝试绘制股价随时间变化的图表(请参见上文).下面的代码确实绘制了开盘"价格,但是当我尝试将X轴日期从顺序日期格式设置为ISO日期时,它会抛出AttributeError
.
I am trying to plot stock prices against time (see above). The code below does plot the "OPEN" prices but as I try to format the X-axis dates from ordinal to ISO dates, it throws AttributeError
.
在绘制OHLC图时,相同的代码可以工作,但是现在不起作用了.
The same code worked while plotting the OHLC graph, but somehow this doesn't work now.
AttributeError:列表"对象没有属性"xaxis"
df_copy = read_stock('EBAY')
fig = plt.figure(figsize= (12,10), dpi = 80)
ax1 = plt.subplot(111)
ax1 = plt.plot(df_copy['Date'], df_copy['Open'], label = 'Open values' )
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
推荐答案
此行:
ax1 = plt.plot(df_copy['Date'], df_copy['Open'], label='Open values')
将您的Axes对象指定为plot
命令返回的艺术家的列表.
Refines your Axes object to be the list of artists returned by the plot
command.
您应该直接使用对象,而不是依靠状态机将艺术家放到Axes上:
Instead of relying on the state machine to put artists on the Axes, you should use your objects directly:
df_copy = read_stock('EBAY')
fig = plt.figure(figsize=(12, 10), dpi=80)
ax1 = fig.add_subplot(111)
lines = ax1.plot(df_copy['Date'], df_copy['Open'], label='Open values')
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
这篇关于Matplotlib绘图:AttributeError:'列表'对象没有属性'xaxis'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!