【问题标题】:Matplotlib Plotting: AttributeError: 'list' object has no attribute 'xaxis'Matplotlib 绘图:AttributeError:“列表”对象没有属性“xaxis”
【发布时间】:2018-01-22 06:48:45
【问题描述】:

Example Plot that needs to format date

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

绘制 OHLC 图形时,相同的代码可以工作,但不知何故,现在这不起作用。

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

【问题讨论】:

  • 试试df_copy.set_index('Date').Open.plot(label='Open values')
  • 行得通!谢谢。但是,我的查询仍然存在。为什么它会抛出错误,指定它的列表?

标签: python pandas matplotlib plot


【解决方案1】:

这一行:

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

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

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

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

【讨论】:

    【解决方案2】:

    问题出在你的写作上

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

    由于您将 ax1 的类型从 plt.subplot() 返回的句柄更改。在所述行之后,它是添加到绘图中的行列表,它解释了您的错误消息。有关 plot 命令请参见纪录片:

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

    【讨论】:

      猜你喜欢
      • 2016-03-27
      • 1970-01-01
      • 2021-07-05
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      • 2016-05-14
      • 2016-12-21
      相关资源
      最近更新 更多