Drawing edge weights with networkx

2,794 views
Skip to first unread message

nk

unread,
May 1, 2011, 8:22:47 AM5/1/11
to networkx-discuss
Hello all,

I am having problems in drawing the weights of edges of the graphs
using networkx and wx

I am writing the following lines :

G=networkx.Graph()
for node in self.state.graph.Graph.keys():
G.add_node(node)
for value in self.state.graph.Graph[node].keys(): #
just the values of nodes
G.add_edge( node , value , weight =
state.graph.Graph[node][value] ) # values of other nodes and the
weight

edge_labels=dict([((u,v,),d['weight'])
for u,v,d in G.edges(data=True)])
pos=networkx.spring_layout(G)
networkx.drawing.nx_pylab.draw_networkx(G,pos,ax =
self.axes)

networkx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels)

Aric Hagberg

unread,
May 1, 2011, 10:32:12 AM5/1/11
to networkx...@googlegroups.com

Can you explain what problem you are having?
Or maybe post an example to run that demonstrates the problem?

Aric

naman

unread,
May 1, 2011, 10:38:51 AM5/1/11
to networkx...@googlegroups.com
The edge weights do not get displayed. I read on the net where the same example was given with the difference that they had used 

edge_labels=dict([((u,v,),d['weight'])    for u,v,d in G.edges(data=True)])
pos=networkx.spring_layout(G)
networkx.draw(G,pos)
networkx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels)

However I used the same to draw on the wx widget and the edge weights do not come. Then i tried the code that I sent earlier which too does not display the weights.

Can somebody help me out....

Thanks


--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To post to this group, send email to networkx...@googlegroups.com.
To unsubscribe from this group, send email to networkx-discu...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/networkx-discuss?hl=en.




--
Naman

naman

unread,
May 1, 2011, 10:55:32 AM5/1/11
to networkx...@googlegroups.com
If anybody does not understand this question then please simply help me by telling how I can draw edge weights using matplotlib and networkx on a wx panel.

Currenltly I am able to draw the graph with the help of draw_networkx but the edge_labels does not work....

Thanks a lot...
--
Naman

Dan Schult

unread,
May 1, 2011, 11:08:08 AM5/1/11
to networkx...@googlegroups.com
I'm not sure from what you say what is going on--- it sounds like a matplotlib issue.
It could be as simple as telling matplotlib to redraw the picture.  Try adding 
draw_if_interactive()
at the end.

Otherwise try plotting something simple in matplotlib and then getting putting some text on it with gca().text(x,y,'hello') and see if that works.  Again, you may have to tell matplotlib to update the picture with draw_if_interactive() or show().
Dan

naman

unread,
May 1, 2011, 1:08:18 PM5/1/11
to networkx...@googlegroups.com
I am also attaching a working code below.... 

It has the same problem that the edges do not get drawn... If anybody can tell me how to do that I would be really grateful.

Thanks a lot...


Python 2.6
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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()
    
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Aric Hagberg

unread,
May 1, 2011, 1:21:48 PM5/1/11
to networkx...@googlegroups.com
On Sun, May 1, 2011 at 11:08 AM, naman <naman...@gmail.com> wrote:
> I am also attaching a working code below....
> It has the same problem that the edges do not get drawn... If anybody can
> tell me how to do that I would be really grateful.
>

That is very helpful. You should pass an ax=self.axes into
draw_networkx_edge_labels like this:

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

On my system your code doesn't even get that far - it breaks with
$ python wx2.py
Traceback (most recent call last):
File "wx2.py", line 52, in <module>
app = App(0)
File "/usr/lib64/python2.6/dist-packages/wx-2.8-gtk2-unicode/wx/_core.py",
line 7978, in __init__
self._BootstrapApp()
File "/usr/lib64/python2.6/dist-packages/wx-2.8-gtk2-unicode/wx/_core.py",
line 7552, in _BootstrapApp
return _core_.PyApp__BootstrapApp(*args, **kwargs)
File "wx2.py", line 47, in OnInit
frame = CanvasFrame()
File "wx2.py", line 39, in __init__
nx.draw_networkx_nodes(G, pos, ax=self.axes)
File "/home/aric/.local/lib/python2.6/site-packages/networkx/drawing/nx_pylab.py",
line 404, in draw_networkx_nodes
pylab.sci(node_collection)
File "/home/aric/.local/lib/python2.6/site-packages/matplotlib/pyplot.py",
line 174, in sci
gca()._sci(im)
File "/home/aric/.local/lib/python2.6/site-packages/matplotlib/axes.py",
line 1360, 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

which looks like a problem with some code in networkx.draw_networkx_nodes().
I'll see if I can understand what that problem is.
I have matplotlib-1.0.0

Aric

Reply all
Reply to author
Forward
0 new messages