Python "'module'对象不可调用”

6tqwzwtp  于 2023-02-18  发布在  Python
关注(0)|答案(6)|浏览(152)

我想画个图:

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

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

tuwxkamq

tuwxkamq1#

figurematplotlib提供的一个模块。
您可以在Matplotlib documentation中了解更多信息
我认为您需要的是matplotlib.figure.Figure(类,而不是模块)
这里有记录

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

cvxl0en22#

您需要执行以下操作:

matplotlib.figure.Figure

这里,

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

参考此处。
所以你得这样称呼它:

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

yuvru6vn3#

要防止将来出现有关matplotlib.pyplot的错误,请尝试执行以下操作:导入matplotlib.pyplot作为plt
如果您使用Jupyter笔记本电脑并用途:%matplotlib内联,请确保“%matplotlib内联跟随导入matplotlib.pyplot作为plt
Karthikr的答案是有效的,但可能无法消除与pyplot模块相关的其他代码行中的错误。
快乐编码!!

j2qf4p5b

j2qf4p5b4#

代替

from matplotlib import *

使用

import matplotlib.pyplot as plt
5rgfhyps

5rgfhyps5#

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

import matplotlib.pyplot as plt
%matplotlib notebook

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

wqnecbli

wqnecbli6#

错误库:
导入matplotlib作为plt
平面图(图尺寸=(7,7))
TypeError:"module"对象不可调用
但是
正确库:
导入matplotlib.pyplot作为plt
平面图(图尺寸=(7,7))
上面的代码为我工作相同的错误.

相关问题