簡體   English   中英

在邊緣繪制權重的程序無法正常工作 + networkx

[英]Program to draw weights in edges not working correctly + networkx

我有這個程序在 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()

感謝 networkx 社區的Mr Aric Hagberg的回答。

但是,自從我了解它以來,我認為我應該在這里為更多用戶回答。

上面代碼中唯一的問題是

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

現在它使用 networkx 在 wx 內給出加權邊緣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM