Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib mpl.figure.figure()失败,而fig,ax=plt.subplot()成功_Python_Matplotlib_Matlab Figure - Fatal编程技术网

Python Matplotlib mpl.figure.figure()失败,而fig,ax=plt.subplot()成功

Python Matplotlib mpl.figure.figure()失败,而fig,ax=plt.subplot()成功,python,matplotlib,matlab-figure,Python,Matplotlib,Matlab Figure,我想使用matplotlib 2.1.2创建matplotlib图形,而不使用pyplot。 此操作失败,并出现属性错误 import matplotlib as mpl fig = mpl.figure.Figure() AttributeError:模块“matplotlib”没有属性“figure” 但是,如果在figure调用之前导入了任何后端,则会成功。 或者,在Jupyter中,如果有%matplotlib内联或%matplotlib小部件,它也会成功 import matplot

我想使用matplotlib 2.1.2创建matplotlib图形,而不使用pyplot。 此操作失败,并出现属性错误

import matplotlib as mpl
fig = mpl.figure.Figure()
AttributeError:模块“matplotlib”没有属性“figure”

但是,如果在figure调用之前导入了任何后端,则会成功。 或者,在Jupyter中,如果有
%matplotlib内联
%matplotlib小部件
,它也会成功

import matplotlib as mpl
import matplotlib.backends.backend_tkagg as tkagg
fig = mpl.figure.Figure()
后端导入如何向matplotlib添加地物? 这是预期的行为吗

pyplot接口似乎没有这种依赖关系:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

无论是否导入后端,都会成功。

这只是直接导入的问题

import matplotlib.figure
fig = matplotlib.figure.Figure()

按预期工作,即确保在使用子模块之前导入它们。

因此导入后端与此没有任何关系?后端代码可能会导入figure子模块,从而使其可用?子模块在使用之前总是需要显式导入,这是一条一般规则,还是这仅适用于matplotlib和某些库。正确。任何子模块都是模块,需要导入模块。但是,通常情况下,包的
\uuuu init\uuuuuuu.py
会为您做这样的事情,因此您可能已经看到了不需要这样做的情况。matplotlib的
\uuu init\uuuuuuuuupy
不适用于
子模块,因此您需要自己完成。这非常有用,谢谢。在我自己的模块中,我总是在init中导入子模块,我只是假设所有库都是这样做的。但你的评论很清楚。