在边缘绘制权重的程序无法正常工作+网络x

发布于 2024-11-04 18:23:15 字数 1453 浏览 2 评论 0原文

我有这个程序在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香草可樂 2024-11-11 18:23:15

感谢 networkx 社区的 Aric Hagberg 先生 提供了此答案。

然而,自从我了解它后,我想我应该在这里为更多用户回答。

上面代码中唯一的问题是

nx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels,ax= self.axes)

现在它使用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

nx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels,ax= self.axes)

Now it gives weighted edges inside wx using networkx.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文