Python“'模块'对象不可调用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16527149/
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-08-18 22:55:52  来源:igfitidea点击:

Python "'module' object is not callable"

pythonmodulematplotlib

提问by rferdinand

I'm trying to make a plot:

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

from matplotlib import *
import sys
from pylab import *

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

But I get this error when I try to execute it:

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

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

I have run a similar script before, and I think I've imported all the relevant modules.

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

采纳答案by Ewan

The figureis a module provided by matplotlib.

figure是所提供的模块matplotlib

You can read more about it in the Matplotlib documentation

您可以在Matplotlib 文档中阅读更多相关信息

I think what you want is matplotlib.figure.Figure(the class, rather than the module)

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

It's documented here

记录在这里

from matplotlib import *
import sys
from pylab import *

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

or

或者

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

or

或者

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

or to get pylabto work without conflicting with matplotlib:

或在pylab不与matplotlib以下冲突的情况下开始工作:

from matplotlib import *
import sys
import pylab as pl

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

回答by karthikr

You need to do:

你需要做:

matplotlib.figure.Figure

Here,

这里,

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

Reference here.

参考这里

So you would have to call it like this:

所以你必须这样称呼它:

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

回答by minion_1

To prevent future errors regarding matplotlib.pyplot, try doing this: import matplotlib.pyplot as plt

为了防止将来出现关于 matplotlib.pyplot 的错误,请尝试这样做: import matplotlib.pyplot as plt

If you use Jupyter notebooks and use: %matplotlib inline, make sure that the "%matplotlib inline FOLLOWS the import matplotlib.pyplot as plt

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

Karthikr's answer works but might not eliminate errors in other lines of code related to the pyplot module.

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

Happy coding!!

快乐编码!!

回答by Pirate X

Instead of

代替

from matplotlib import *

use

import matplotlib.pyplot as plt