Drawing nodes with attributes

864 views
Skip to first unread message

George

unread,
Jul 1, 2010, 5:28:01 PM7/1/10
to networkx-discuss
Hi, I'm new to networkx and python. The following code doesn't work:

# test.py
#
import networkx as nx
import matplotlib.pyplot as plt
import random as rd

G = nx.MultiDiGraph()

for i in range(10):
G.add_node(i)
G.add_edge(rd.randint(0,9),rd.randint(0,9))
G[i]['value'] = rd.random()
nx.draw(G,node_color=[rd.random() for _ in range(10)])
plt.show()
#
#


I get the following error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test.py", line 14, in <module>
nx.draw(G,node_color=[rd.random() for _ in range(10)])
File "/usr/local/lib/python2.6/dist-packages/networkx-1.2.dev-
py2.6.egg/networkx/drawing/nx_pylab.py", line 138, in draw
draw_networkx(G,pos=pos,ax=ax,**kwds)
File "/usr/local/lib/python2.6/dist-packages/networkx-1.2.dev-
py2.6.egg/networkx/drawing/nx_pylab.py", line 270, in draw_networkx
pos=nx.drawing.spring_layout(G) # default to spring layout
File "/usr/local/lib/python2.6/dist-packages/networkx-1.2.dev-
py2.6.egg/networkx/drawing/layout.py", line 211, in
fruchterman_reingold_layout
A=nx.to_numpy_matrix(G)
File "/usr/local/lib/python2.6/dist-packages/networkx-1.2.dev-
py2.6.egg/networkx/convert.py", line 651, in to_numpy_matrix
for u,v,attrs in G.edges_iter(data=True):
File "/usr/local/lib/python2.6/dist-packages/networkx-1.2.dev-
py2.6.egg/networkx/classes/multidigraph.py", line 387, in edges_iter
for key,data in keydict.iteritems():
AttributeError: 'float' object has no attribute 'iteritems'

The problem is with the attribute ['value']. If I remove it the code
works. How can I draw the network and at the same time have nodes with
attributes?

Thanks,

George.

Richard Careaga

unread,
Jul 1, 2010, 8:54:55 PM7/1/10
to networkx...@googlegroups.com
http://docs.python.org/library/random.html: random() returns a floating
point number (type = FLOAT) and you can't iterate over a float. Try
coercing to a string:

George wrote:
> G[i]['value'] = str(rd.random())

Dan Schult

unread,
Jul 2, 2010, 12:12:19 AM7/2/10
to networkx...@googlegroups.com
When using MultiDiGraph (or MultiGraph) you can't set edge data without referring to the edge Key.
Also to set node attributes use G.node[i]['value'] instead of G[i]['value']

I can't tell if your lin G[i]['value']=rd.random() is intended to set an edge value or a node value.
It should be either
G[i][j]['weight']=rd.random() # edge value
or
G.node[i]['value']=rd.random()

Dan

> --
> 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.
>

George Bezerra

unread,
Jul 2, 2010, 12:11:48 PM7/2/10
to networkx...@googlegroups.com
Thanks Dan!

"G.node[i]['value']=rd.random()" works just fine.

George.
--
George Bezerra
Doctoral Student
Department of Computer Science
University of New Mexico
Reply all
Reply to author
Forward
0 new messages