14

我在这里查看了有关此主题的其他帖子,但没有找到明确的答案,尽管我确信它很简单。

我的代码具有以下结构...

import matplotlib
...
...

class xyz:
    def function_A(self,...)
        ...
        ...
        fig1 = matplotlib.figure()
        ...
        ...

我正在从“xyz”的实例中调用“function_A”,当我这样做时,我收到错误消息:

AttributeError: 'module' object has no attribute 'figure'

根据我读过的帖子,我导入 matplotlib 的方式似乎有问题,但我无法解决。我已经尝试在 Function_A 定义中导入它(我认为这是不好的形式,但我想测试它),但我仍然是同样的错误。

我在其他地方使用了我的“function_A”代码没有问题,但它只是模块中的一个函数,而不是类中的方法。

任何帮助表示赞赏!

4

1 回答 1

28

我认为你是对的,这是一个进口问题。该模块matplotlib没有功能figure

>>> import matplotlib
>>> matplotlib.figure
Traceback (most recent call last):
  File "<ipython-input-130-82eb15b3daba>", line 1, in <module>
    matplotlib.figure
AttributeError: 'module' object has no attribute 'figure'

图形功能位于更深的位置。有几种方法可以将其拉入,但通常的导入看起来更像:

>>> import matplotlib.pyplot as plt
>>> plt.figure
<function figure at 0xb2041ec>

坚持这种习惯可能是一个好主意,因为您可以在 Web 上找到的大多数示例都使用它,例如matplotlib 库中的示例。(当我需要弄清楚如何做某事时,画廊仍然是我去的第一个地方:我找到一个看起来像我想要的图像,然后查看代码。)

于 2013-04-19T16:32:32.737 回答