pandas Matplotlib 绘图:AttributeError:'list' 对象没有属性 'xaxis'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48376000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:04:58  来源:igfitidea点击:

Matplotlib Plotting: AttributeError: 'list' object has no attribute 'xaxis'

pythonpandasmatplotlibplot

提问by MuizVhora

Example Plot that needs to format date

需要格式化日期的示例图

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.

我试图根据时间绘制股票价格(见上文)。下面的代码确实绘制了“OPEN”价格,但是当我尝试将 X 轴日期从序数格式化为 ISO 日期时,它抛出AttributeError.

The same code worked while plotting the OHLC graph, but somehow this doesn't work now.

绘制 OHLC 图时相同的代码有效,但不知何故现在不起作用。

AttributeError: 'list' object has no attribute 'xaxis'

AttributeError: 'list' 对象没有属性 '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'))

采纳答案by Paul H

This line:

这一行:

ax1 = plt.plot(df_copy['Date'], df_copy['Open'], label='Open values')

ax1 = plt.plot(df_copy['Date'], df_copy['Open'], label='Open values')

Refines your Axes object to be the list of artists returned by the plotcommand.

将 Axes 对象细化为plot命令返回的艺术家列表。

Instead of relying on the state machine to put artists on the Axes, you should use your objects directly:

您应该直接使用您的对象,而不是依赖状态机将艺术家放在 Axes 上:

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

回答by FlyingTeller

The problem comes from you writing

问题出在你写作上

ax1 = plt.plot(df_copy['Date'], df_copy['Open'], label = 'Open values' )

Since you are changing the type of ax1 from being the handle returned by plt.subplot(). After said line, it is a list of lines that were added to the plot, which explains your error message. See the documentary on the plot command:

由于您正在将 ax1 的类型从plt.subplot(). 在该行之后,它是添加到绘图中的行列表,它解释了您的错误消息。看关于 plot 命令的纪录片:

Return value is a list of lines that were added. matplotlib.org

返回值是添加的行列表。 matplotlib.org