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

Matplotlib mpl.figure.Figure() fails, whereas fig,ax=plt.subplots() succeeds

我想使用 matplotlib 2.1.2 创建一个 matplotlib 而不使用 pyplot。 这会因属性错误而失败。

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

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

但是,如果有任何后端的导入,在图形调用之前,它会成功。 或者在 Jupyter 中,如果有 %matplotlib inline%matplotlib widget 它也会成功。

import matplotlib as mpl
import matplotlib.backends.backend_tkagg as tkagg
fig = mpl.figure.Figure()

一个后端的import怎么给matplotlib加个? 这是预期的行为吗?

pyplot接口好像没有这样的依赖:

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

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

这只是直接导入的问题。

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

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