Help on drawing networks

108 views
Skip to first unread message

Mauro Cavalcanti

unread,
Jul 13, 2009, 9:34:53 AM7/13/09
to networkx...@googlegroups.com
Dear ALL,

I have been trying to create a graph superimposed on a MPL/Basemap,
following an example Aric kindly provided several months ago.

The sample code below reads a text file comprised of graph edges and
its coordinates in geographic space (lats, longs), creates a
dictionary and then attempts to plot the graph over the basemap with a
call to draw_networkx. However, it fails with a
networkx.exception.NetworkXError: Node 1 has no position.

The file is correctly read and the graph edges also seem to be
correctly created from it, but it looks networkx is not able to find
out the initial node form where to draw the graph (?), other than this
(and my own naiveté) I cannot figure out what the problem may be.

Any hints and suggestions would be very welcome.

With best regards,

------ Sample code

import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap as Basemap

m = Basemap()

# position in decimal lat/lon
coords = np.loadtxt("test.dat", usecols=(1,3))
lats = coords[:,0]
lons = coords[:,1]

# convert lat and lon to map projection
mx, my = m(lons, lats)

# The NetworkX part
# put map projection coordinates in pos dictionary
G = nx.Graph()
edges = np.loadtxt("test.dat", usecols=(0,2), dtype=np.int16)
G.add_edges_from(edges)
pos = {}
for i in range(len(coords)):
pos[str(i)] = (mx[i], my[i])
print pos

# draw graph
nx.draw_networkx(G, pos, node_size=200)

# Now draw the map
m.drawcoastlines()
m.drawcountries()
plt.show()

------ Sample data

5 -63.1167 1 10.55
4 -63.1167 5 7.91667
11 -65.9667 5 10.3167
10 -67.4333 11 8.93333
14 -68.4 10 10.6167
3 -70.6667 14 9.53333
13 -72.8667 3 9.83333
15 -72.9833 13 10.6167
8 -60.5 4 3.93333
7 -56.4833 8 3.1
12 -78.9333 13 8.38333
2 -79.4333 12 9.15
9 -81.0167 2 7.66667
6 -81.1833 9 7.51667


--
Dr. Mauro J. Cavalcanti
Ecoinformatics Studio
P.O. Box 46521, CEP 20551-970
Rio de Janeiro, RJ, BRASIL
E-mail: maur...@gmail.com
Web: http://studio.infobio.net
Linux Registered User #473524 * Ubuntu User #22717
"Life is complex. It consists of real and imaginary parts."

Aric Hagberg

unread,
Jul 13, 2009, 11:48:55 AM7/13/09
to networkx...@googlegroups.com

I can't tell from looking at only part of your data but my guess is
that the labels in the "pos" dictionary do not match up exactly with
the labels of your nodes. That is, the keys in "pos" should be the
same as the nodes in G. Also make sure they are both the same type,
e.g. integers or strings.

Aric

Reply all
Reply to author
Forward
0 new messages