在边缘绘制权重的程序无法正常工作+网络x
我有这个程序在 wx 框架内使用 matplotlib 绘制带有节点的标记边缘。
我使用网站上的示例和其他人提出的问题将其结合起来。
但它无法正常工作,因为节点和边确实被绘制,但权重却没有绘制。
有人可以帮我找出原因吗...
import wxversion
wxversion.ensureMinimal('2.8')
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure
import wx
import networkx as nx
class CanvasFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,
'CanvasFrame',size=(550,350))
self.SetBackgroundColour(wx.NamedColor("WHITE"))
self.figure = Figure()
self.axes = self.figure.add_subplot(111)
self.canvas = FigureCanvas(self, -1, self.figure)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.SetSizer(self.sizer)
self.Fit()
G = nx.Graph()
G.add_edge(1,3,weight = 5)
G.add_edge(1,2,weight = 4)
pos = nx.spring_layout(G)
nx.draw_networkx(G, pos, ax=self.axes)
edge_labels=dict([((u,v,),d['weight'])
for u,v,d in G.edges(data=True)])
nx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels)
class App(wx.App):
def OnInit(self):
'Create the main window and insert the custom frame'
frame = CanvasFrame()
frame.Show(True)
return True
app = App(0)
app.MainLoop()
I have this program to draw a labelled edge with the nodes using matplotlib inside a wx frame.
I have combined it using examples at the site and queries asked by other people.
But it isn't working correctly as the nodes and edges do get drawn but the weights do not.
Can somebody help me find the reason for it...
import wxversion
wxversion.ensureMinimal('2.8')
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure
import wx
import networkx as nx
class CanvasFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,
'CanvasFrame',size=(550,350))
self.SetBackgroundColour(wx.NamedColor("WHITE"))
self.figure = Figure()
self.axes = self.figure.add_subplot(111)
self.canvas = FigureCanvas(self, -1, self.figure)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.SetSizer(self.sizer)
self.Fit()
G = nx.Graph()
G.add_edge(1,3,weight = 5)
G.add_edge(1,2,weight = 4)
pos = nx.spring_layout(G)
nx.draw_networkx(G, pos, ax=self.axes)
edge_labels=dict([((u,v,),d['weight'])
for u,v,d in G.edges(data=True)])
nx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels)
class App(wx.App):
def OnInit(self):
'Create the main window and insert the custom frame'
frame = CanvasFrame()
frame.Show(True)
return True
app = App(0)
app.MainLoop()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢 networkx 社区的
Aric Hagberg 先生
提供了此答案。然而,自从我了解它后,我想我应该在这里为更多用户回答。
上面代码中唯一的问题是
现在它使用networkx在wx内给出加权边。
Credits to
Mr Aric Hagberg
of networkx community for this answer.However since I came to know about it I thought I should answer here for further users.
The only problem in above code is
Now it gives weighted edges inside wx using networkx.