【问题标题】:Python "'module' object is not callable"Python“'模块'对象不可调用”
【发布时间】:2021-10-18 18:12:51
【问题描述】:

我正在尝试制作一个情节:

from matplotlib import *
import sys
from pylab import *

f = figure ( figsize =(7,7) )

但是当我尝试执行它时出现此错误:

  File "mratio.py", line 24, in <module>
    f = figure( figsize=(7,7) )
TypeError: 'module' object is not callable

我之前运行过一个类似的脚本,我想我已经导入了所有相关的模块。

【问题讨论】:

  • 你有没有用其他东西影响figure
  • 运行import pylab; print pylab.__file__ 并给我们结果
  • /home/apps/fas/Langs/Python/2.7.2/lib/python2.7/site-packages/matplotlib/pylab.pyc
  • print pylab.figure 怎么样?
  • “/home/apps/fas/Langs/Python/2.7.2/lib/python2.7/site-packages/matplotlib/pylab.p‌​y”的内容是什么?

标签: python module matplotlib


【解决方案1】:

figurematplotlib提供的模块。

您可以在Matplotlib documentation了解更多信息

我认为你想要的是matplotlib.figure.Figure(类,而不是模块)

我是documented here

from matplotlib import *
import sys
from pylab import *

f = figure.Figure( figsize =(7,7) )

from matplotlib import figure
f = figure.Figure( figsize =(7,7) )

from matplotlib.figure import Figure
f = Figure( figsize =(7,7) )

或让pylab 工作而不与matplotlib 冲突:

from matplotlib import *
import sys
import pylab as pl

f = pl.figure( figsize =(7,7) )

【讨论】:

  • 但是 figure 应该是来自 pylab 的函数,所以这应该仍然有效。
  • @WinstonEwert 我添加了另一个解决方案,允许pylab 导入图形而不与matplotlib 冲突
  • 但这并不能解释为什么来自pylabfigure 没有取代matplotlib 的数字。应该有。
  • 我也想知道。我之前没有调用“figure.Figure”来绘制图形。
  • 在它自己的代码 sn-p 上使用 Python v3.5,但是如果我更改导入顺序,在 matplotlib 之前加载 pylab,我会收到错误消息。所以顺序很重要。最好按照@Ewan 的建议明确指定pl.figure 以避免这种情况
【解决方案2】:

你需要做的:

matplotlib.figure.Figure

这里,

matplotlib.figure is a package (module), and `Figure` is the method

参考here

所以你必须这样称呼它:

f = figure.Figure(figsize=(7,7))

【讨论】:

  • @minion_1 的解决方案是实际解决方案,这里是 2013 解决方案。
【解决方案3】:

为防止将来出现有关 matplotlib.pyplot 的错误,请尝试执行以下操作: 将 matplotlib.pyplot 导入为 plt

如果您使用 Jupyter 笔记本并使用:%matplotlib inline,请确保“%matplotlib inline FOLLOWS import matplotlib.pyplot as plt

Karthikr 的回答有效,但可能无法消除与 pyplot 模块相关的其他代码行中的错误。

编码愉快!!

【讨论】:

    【解决方案4】:

    代替

    from matplotlib import *
    

    使用

    import matplotlib.pyplot as plt
    

    【讨论】:

    • minion_1 之前已经说明了这一点。您仍然为更改提供了格式清晰的建议。
    【解决方案5】:

    对于 Jupyter 笔记本,我通过 importig matplotlib.pyplot 解决了这个问题。

    import matplotlib.pyplot as plt
    %matplotlib notebook
    

    现在可以使用f = plt.figure() 制作绘图。

    【讨论】:

      猜你喜欢
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 2017-01-12
      • 2021-06-12
      • 1970-01-01
      相关资源
      最近更新 更多