Re: [networkx-discuss] Trouble drawing weighted graph (in convert.py: float object has no attribute

116 views
Skip to first unread message

jmsiqueiro...@gmail.com

unread,
Nov 8, 2012, 12:55:32 PM11/8/12
to networkx...@googlegroups.com
On , Ryan Acumen <ryan....@gmail.com> wrote:
> When I try to draw a graph I've created from a file in this manner:
> >>> import networkx as nx
> >>> G = nx.Graph()
> >>> prod_parse(G,'/home/acumen/Programs/diode.prod')
> >>> nx.draw(G)
>
> I get this error:
> Traceback (most recent call last):
>   File "", line 1, in
>     nx.draw(G)
>   File "/usr/lib/python2.7/site-packages/networkx-1.7-py2.7.egg/networkx/drawing/nx_pylab.py", line 133, in draw
>     draw_networkx(G,pos=pos,ax=ax,**kwds)
>   File "/usr/lib/python2.7/site-packages/networkx-1.7-py2.7.egg/networkx/drawing/nx_pylab.py", line 264, in draw_networkx
>     pos=nx.drawing.spring_layout(G) # default to spring layout
>   File "/usr/lib/python2.7/site-packages/networkx-1.7-py2.7.egg/networkx/drawing/layout.py", line 236, in fruchterman_reingold_layout
>     A=nx.to_numpy_matrix(G,weight=weight)
>   File "/usr/lib/python2.7/site-packages/networkx-1.7-py2.7.egg/networkx/convert.py", line 524, in to_numpy_matrix
>     M[index[u],index[v]]=d.get(weight,1)
> AttributeError: 'float' object has no attribute 'get'
>
> Here is my code to build the graph:
> for i in e1:
>         t = i.split(',')
>         edges += [(t[0],t[1],float(t[2]))]
>     #convert attributes
>     for i in a1:
>         t = i.split(',')
>         attributes += [[t[0],t[1],float(t[2])]]
>     #build graph
>     G.add_nodes_from(nodes)
>
> I have verified that the edge tuples are structured correctly, at least to my understanding:
> edges: [('Diode', 'Die', 0.000717646120724),('Diode', 'Case', 0.465672593892),('Diode', 'Lead Frame', 0.498684315445),('Diode', 'Die Attach', 0.0318156446854),('Diode', 'Plating', 0.00310979985647),('Die', 'Silicon', 1.0),('Case', 'Silica', 0.865),('Case', 'Epoxy', 0.07),('Case', 'Aluminum Hydroxide', 0.015),('Case', 'Bisphenol A', 0.05),('Lead Frame', 'Copper', 1.0),('Die Attach', 'Lead', 0.925),('Die Attach', 'Gold', 0.025),('Die Attach', 'Tin', 0.05),('Plating', 'Tin', 1.0)]
>
> I'm sure the error is on my end.  Anyone know what my problem may be?
>
>
>
>
>
> --
>
> You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
>
> To view this discussion on the web visit https://groups.google.com/d/msg/networkx-discuss/-/s6FEKxu311cJ.
>
> 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.
>
>
>
>

jmsiqueiro...@gmail.com

unread,
Nov 8, 2012, 1:36:20 PM11/8/12
to networkx...@googlegroups.com
Hi Ryan:

I am new on networkX so my suggestion might be a bit dumb. I tried to generate a weighted network with your list 'edges' (as it is in your message) doing this:

import networkx as nx
import pylab as pl

g = nx.Graph()
edges = ['your list']
g.add_edges_from('edges')

and it returns an error:

ypeError Traceback (most recent call last)
/home/txuma/<ipython-input-32-6098c8e1886d> in <module>()
----> 1 g.add_edges_from(edges)

/usr/lib/pymodules/python2.7/networkx/classes/graph.pyc in add_edges_from(self, ebunch, attr_dict, **attr)
787 datadict=self.adj[u].get(v,{})
788 datadict.update(attr_dict)
--> 789 datadict.update(dd)
790 self.adj[u][v] = datadict
791 self.adj[v][u] = datadict

TypeError: 'float' object is not iterable

So I modified your list (a sample of it) adding the dictionary format to the weight attribute of the edge (notice the keys {}): ('Lead Frame', 'Copper', {'weight':1.0}), and it worked:

g = nx.Graph()
edges = [('Lead Frame', 'Copper', {'weight':1.0}),('Die Attach', 'Lead', {'weight':0.925}),('Die Attach', 'Gold', {'weight':0.025}),('Die Attach', 'Tin', {'weight':0.05}),('Plating', 'Tin', {'weight':1.0})]
g.add_edges_from('edges')
nx.draw(g)
pl.show()

I guess i understood your problem but I am might be wrong. Still, I hope this helps.

Cheers

JM
Reply all
Reply to author
Forward
0 new messages