سؤال

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()
هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top