python - NetworkX 和 Matplotlib - 轴错误

标签 python matplotlib networkx

我在 Windows 上有 Networkx 1.6 和 Matplotlib 1.1.0,这是我的代码:

self.figure = Figure()
self.axes = self.figure.add_subplot(1,1,1)
self.canvas = FigureCanvas(self, -1, self.figure)
G = nx.Graph()
G.add_node(6)
pos = nx.spring_layout(G)
nx.draw(G, pos, ax = self.axes)

我收到错误:

File "C:\Python27\lib\site-packages\matplotlib\axes.py, line 1374, in _sci
"Argument must be an image, collection, or ContourSet in this Axes"
ValueError: Argument must be an image, collection, or ContourSet in this Axes

有人知道怎么解决吗?

最佳答案

对于 matplotlib 1.0+,不要使用Figure(),而使用 pyplot.figure()。 Figure() 创建一个Figure,但不会将其注册到 pyplot 内的FigManager,而 pyplot.figure() 会注册。

在绘图函数中,它们通过调用 gcf() 来获取图形,gcf() 返回当前图形,如果不存在,则创建一个图形。

稍后对 sci() 的调用将尝试通过调用 gca() 来验证您应用于绘图函数的位置(集合)确实已经在轴上注册,但由于您有一个新的 图,因此没有轴,它会引发异常。

我将其称为 matplotlib bug。

我还没有阅读 matplotlib 的更改说明,那里可能有描述。我通过调试 matplotlib 代码发现了这一点。

关于python - NetworkX 和 Matplotlib - 轴错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8644233/

相关文章:

Python - 日期时间时间始终为零

python - Cython 程序比普通 Python 慢(10M 选项 3.5s vs 3.25s Black Scholes)——我错过了什么?

python - 如何让机器人在 discord.py 中编辑自己的消息

pandas - Matplotlib::不显示所有 x 轴数据框变量

python - 为 Networkx 图添加标题?

python - 在 C++ 中使用加载的 tensorflow 模型运行推理

python-3.x - 多处理显示 matplotlib 图

python - 使用 matplotlib 时设置 xlim 和 ylim(有些奇怪)

python - NetworkX 访问具有多个节点属性的节点

python - NetworkX 非对称权重