NetworkX和Matplotlib - 轴错误

时间:2011-12-27 11:40:00

标签: 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

有人知道如何修复它吗?

4 个答案:

答案 0 :(得分:2)

对于matplotlib 1.0+,请不要使用Figure(),请使用pyplot.figure()。图()制作了一个图,但没有用pyplot中的figManager注册它,pyplot.figure()的确如此。

在绘图函数中,他们通过调用gcf()来获取图形,并且gcf()返回当前图形,或者如果不存在则创建 new

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

我将这称为matplotlib错误。

我还没有看过matplotlib的更改说明,可能会在那里描述。我通过调试matplotlib代码找到了。

答案 1 :(得分:1)

有没有办法在tkinter面板/主窗口中停靠/嵌入pyplot.show()命令?或者它总是弹出自己的窗口?

def Embedded_Graph(Parent, G):
 Parent.figure = Figure()
 Parent.axe = Parent.figure.add_subplot(1,1,1)
 pos = nx.spring_layout(G)
 nx.draw(G, pos)
 pyplot.show()

答案 2 :(得分:1)

@Carel,我希望你找到了你搜索的内容。如果没有,这里是如何在Tkinter画布上嵌入networkx图的示例:

def embed_graph(G):
    pos = nx.spring_layout(G)
    nx.draw(G, pos)
    canvas = FigureCanvasTkAgg(plt.figure(1), master=self)
    canvas.show()
    canvas.get_tk_widget().pack(side="top")

答案 3 :(得分:0)

我不确定你想要绘制什么,但是你得到了一个节点的图:

    self.figure = Figure()
    self.axe = self.figure.add_subplot(1,1,1)
    G = nx.Graph()
    G.add_node(6)
    pos = nx.spring_layout(G)
    nx.draw(G, pos)
    pyplot.show()

因此,删除明显正确的ax参数可以绘制图形。我发现post here显示与ax参数相关的相同错误。它似乎正在mpl 0.99上工作但不在mpl 1.0