[networkx-discuss] Drawing problems form a GML file

127 views
Skip to first unread message

Pierre Besson

unread,
Mar 12, 2012, 8:09:33 AM3/12/12
to networkx...@googlegroups.com
Dear all,

I would like to plot a graph generated from matlab and saved as a GML file. All the steps work fine but when I draw the graph, displayed edges do not correspond to the edges defined in the GML file.

Please find the attached file Test_graph.gml. This file defines a graph with 164 nodes and 6300 edges.The width of all edges but 3 was set to zero. The 3 remaining edges (width=2) should connect Node 5 with Node 37, Node 5 with Node 39 and Node 5 with 66.

Here is what I have written to draw the graph :

import matplotlib.pyplot as plt
import networkx as nx

G=nx.read_gml('Test_graph.gml');
X=nx.get_node_attributes(G, 'X').values()
Y=nx.get_node_attributes(G, 'Y').values()
Z=nx.get_node_attributes(G, 'Z').values()

pos={}
Nnode=len(X)
for node in range(0,Nnode):
  pos[node+1]=[X[node],Y[node]]

n_dr = nx.get_node_attributes(G, 'sahd_dr').values()
e_width = nx.get_edge_attributes(G, 'width').values()

Lab = nx.get_node_attributes(G, 'name').values()
Label = {}
for node in range(0,Nnode):
  Label[node+1] = Lab[node]

nx.draw_networkx_nodes(G,pos, node_size=n_dr)
nx.draw_networkx_edges(G,pos, width=e_width)
nx.draw_networkx_labels(G, pos, labels=Label)

plt.show()


Do you know why I have this indexing mismatch ?

Thank you,

Pierre Besson
Test_graph.gml

Aric Hagberg

unread,
Mar 18, 2012, 8:47:21 PM3/18/12
to networkx...@googlegroups.com
On Mon, Mar 12, 2012 at 6:09 AM, Pierre Besson <besson...@gmail.com> wrote:
> Dear all,
>
> I would like to plot a graph generated from matlab and saved as a GML file.
> All the steps work fine but when I draw the graph, displayed edges do not
> correspond to the edges defined in the GML file.

You should specify both the edges and the widths in nx.draw_networkx_edges().
e.g. use below

>>> width = nx.get_edge_attributes(G, 'width')
and
>>> nx.draw_networkx_edges(G,pos, edgelist=width.keys(), width=width.values())

>
> import matplotlib.pyplot as plt
> import networkx as nx
>
> G=nx.read_gml('Test_graph.gml');
> X=nx.get_node_attributes(G, 'X').values()
> Y=nx.get_node_attributes(G, 'Y').values()
> Z=nx.get_node_attributes(G, 'Z').values()
>
> pos={}
> Nnode=len(X)
> for node in range(0,Nnode):
>   pos[node+1]=[X[node],Y[node]]
>
> n_dr = nx.get_node_attributes(G, 'sahd_dr').values()
> e_width = nx.get_edge_attributes(G, 'width').values()
>
> Lab = nx.get_node_attributes(G, 'name').values()
> Label = {}
> for node in range(0,Nnode):
>   Label[node+1] = Lab[node]
>
> nx.draw_networkx_nodes(G,pos, node_size=n_dr)
> nx.draw_networkx_edges(G,pos, width=e_width)
> nx.draw_networkx_labels(G, pos, labels=Label)
>
> plt.show()

Aric

Reply all
Reply to author
Forward
0 new messages