【问题标题】:The command fig.show() returns an error in pyplot命令 fig.show() 在 pyplot 中返回错误
【发布时间】:2020-05-09 10:24:37
【问题描述】:

以下代码产生错误:

x = np.arange(100)

fig = plt.subplots()
plt.plot(x)
plt.show()

fig.show()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-528-6ebedab27258> in <module>
----> 1 fig.show()

AttributeError: 'tuple' object has no attribute 'show'

导致错误的原因以及我应该如何更正我的代码?

【问题讨论】:

  • 在错误fig.show() 中是错误的行并且在代码中没有这样的行?究竟是什么错误?
  • 我想绘制调用 fig.show() 的图形。而不是产生情节,我得到一个错误。

标签: python python-3.x matplotlib


【解决方案1】:

plt.subplots,是returns

图:图

axes.Axes 对象或 Axes 对象数组。

因此,如果您仅将 plt.subplots() 的结果分配给 fig,则这两个对象存储在一个元组中,并且您无法更改该元组,即无法将图分配给轴。所以通常这样做更有意义:

x = np.arange(100)
fig, ax = plt.subplots()
ax = plt.plot(x)
fig.show()

【讨论】:

    猜你喜欢
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 2021-02-26
    • 2019-06-22
    • 2014-08-01
    • 1970-01-01
    相关资源
    最近更新 更多