You are on page 1of 8

6/27/2014

Creating Network Graphs with Python - Udacity


p P

1 p P
1.1 vv

' f ?
V .m

1.2 X
1.2.1 -m b
1.2.2
1.2.3 x b b
1.2.4
1.3 X xmp
1.4 3 p Mv
1.5 Mv
1.5.1 -m b
1.5.2
1.5.3 x b b
1.5.4
1.6 Mv xmp

vv
X b p p p . ' p
vpm vm. pb X ' . F 3 p f
Mv .

https://www.udacity.com/wiki/creating-network-graphs-with-python#Overview

1/8

6/27/2014

Creating Network Graphs with Python - Udacity

X
-m b
p b X:
P
PX


' p, mp mpb, f ' v m. x' *. f fm
b _ fm mm- , ..:
$easy_installnetworkx1.7rc1py2.7.egg

x b b
Fm m f mm:
$sudoaptgetinstallpythonnetworkx

P 2.7 mm v b pp b m f X' p.
pf f f pp P b f:
v

https://www.udacity.com/wiki/creating-network-graphs-with-python#Overview

2/8

6/27/2014

Creating Network Graphs with Python - Udacity

Fm mm pmp (m) f mm:


C:\>pypminstallnetworkx

P f F
f p f:
p b f .
mp fm .
mpb fm .
F x' *. f fm b _ fm mm pmp, ..:
C:\>cdC:\folder\where\your\egg\file\is\located
C:\folder\where\your\egg\file\is\located>easy_installnetworkx1.7rc1py2.7.egg

X xmp
xmp b f . p f xmp f b.
v, v mp p:
importnetworkxasnx
importmatplotlib.pyplotasplt
defdraw_graph(graph):
#extractnodesfromgraph
nodes=set([n1forn1,n2ingraph]+[n2forn1,n2ingraph])
#createnetworkxgraph
G=nx.Graph()
#addnodes
fornodeinnodes:
G.add_node(node)
#addedges
foredgeingraph:
G.add_edge(edge[0],edge[1])
#drawgraph
pos=nx.shell_layout(G)
nx.draw(G,pos)
#showgraph
plt.show()
#drawexample
graph=[(20,21),(21,22),(22,23),(23,24),(24,25),(25,20)]
draw_graph(graph)

https://www.udacity.com/wiki/creating-network-graphs-with-python#Overview

3/8

6/27/2014

Creating Network Graphs with Python - Udacity

m p xmp, m f p' pp (, , ,
):
importnetworkxasnx
importmatplotlib.pyplotasplt
defdraw_graph(graph,labels=None,graph_layout='shell',
node_size=1600,node_color='blue',node_alpha=0.3,
node_text_size=12,
edge_color='blue',edge_alpha=0.3,edge_tickness=1,
edge_text_pos=0.3,
text_font='sansserif'):
#createnetworkxgraph
G=nx.Graph()
#addedges
foredgeingraph:
G.add_edge(edge[0],edge[1])
#thesearedifferentlayoutsforthenetworkyoumaytry
#shellseemstoworkbest
ifgraph_layout=='spring':
graph_pos=nx.spring_layout(G)
elifgraph_layout=='spectral':
graph_pos=nx.spectral_layout(G)
elifgraph_layout=='random':
graph_pos=nx.random_layout(G)
else:
graph_pos=nx.shell_layout(G)
#drawgraph
nx.draw_networkx_nodes(G,graph_pos,node_size=node_size,
alpha=node_alpha,node_color=node_color)
nx.draw_networkx_edges(G,graph_pos,width=edge_tickness,
alpha=edge_alpha,edge_color=edge_color)
nx.draw_networkx_labels(G,graph_pos,font_size=node_text_size,
font_family=text_font)
iflabelsisNone:
labels=range(len(graph))
edge_labels=dict(zip(graph,labels))
nx.draw_networkx_edge_labels(G,graph_pos,edge_labels=edge_labels,
label_pos=edge_text_pos)
#showgraph
plt.show()

https://www.udacity.com/wiki/creating-network-graphs-with-python#Overview

4/8

6/27/2014

Creating Network Graphs with Python - Udacity

graph=[(0,1),(1,5),(1,7),(4,5),(4,8),(1,6),(3,7),(5,9),
(2,4),(0,4),(2,5),(3,6),(8,9)]
#youmaynameyouredgelabels
labels=map(chr,range(65,65+len(graph)))
#draw_graph(graph,labels)
#ifedgelabelsisnotspecified,numericlabels(0,1,2...)willbeused
draw_graph(graph)

3 p Mv
m Mv x pb f X p v 3 p:

Mv
-m b
P b Mv:
P
PX


f pfm b f .

x b b
F b v f Mv' p b x f mm fm m:

https://www.udacity.com/wiki/creating-network-graphs-with-python#Overview

5/8

6/27/2014

Creating Network Graphs with Python - Udacity

sudoaptgetinstallpythonwxgtk2.8pythonwxtoolswx2.8i18npythonvtkpythonsetuptoolspythonnum
pypythonconfigobjpythonnetworkx

F Mv b x f mm:
sudoeasy_install"Mayavi[app]"

' v p:
p b f .
mp fm .
mpb fm .
V fm .
xP fm .
F Mv b x f mm fm mm-pmp (m):
C:>easy_installSphinxEnvisageCoreEnvisagePluginsconfigobj
C:>easy_installMayavi[app]

Mv xmp
f xmp p pb:
importnetworkxasnx
importmatplotlib.pyplotasplt
importnumpyasnp
frommayaviimportmlab
importrandom
defdraw_graph3d(graph,graph_colormap='winter',bgcolor=(1,1,1),
node_size=0.03,
edge_color=(0.8,0.8,0.8),edge_size=0.002,
text_size=0.008,text_color=(0,0,0)):
H=nx.Graph()
#addedges
fornode,edgesingraph.items():
foredge,valinedges.items():
ifval==1:
H.add_edge(node,edge)
G=nx.convert_node_labels_to_integers(H)
graph_pos=nx.spring_layout(G,dim=3)
#numpyarrayofx,y,zpositionsinsortednodeorder
xyz=np.array([graph_pos[v]forvinsorted(G)])
#scalarcolors
scalars=np.array(G.nodes())+5
mlab.figure(1,bgcolor=bgcolor)
mlab.clf()
pts=mlab.points3d(xyz[:,0],xyz[:,1],xyz[:,2],
scalars,
scale_factor=node_size,
scale_mode='none',
colormap=graph_colormap,

https://www.udacity.com/wiki/creating-network-graphs-with-python#Overview

6/8

6/27/2014

Creating Network Graphs with Python - Udacity

resolution=20)
fori,(x,y,z)inenumerate(xyz):
label=mlab.text(x,y,str(i),z=z,
width=text_size,name=str(i),color=text_color)
label.property.shadow=True
pts.mlab_source.dataset.lines=np.array(G.edges())
tube=mlab.pipeline.tube(pts,tube_radius=edge_size)
mlab.pipeline.surface(tube,color=edge_color)
mlab.show()#interactivewindow
#createtangledhypercube
defmake_graph(nodes):
defmake_link(graph,i1,i2):
graph[i1][i2]=1
graph[i2][i1]=1
n=len(nodes)
ifn==1:return{nodes[0]:{}}
nodes1=nodes[0:n/2]
nodes2=nodes[n/2:]
G1=make_graph(nodes1)
G2=make_graph(nodes2)
#mergeG1andG2intoasinglegraph
G=dict(G1.items()+G2.items())
#linkG1andG2
random.shuffle(nodes1)
random.shuffle(nodes2)
foriinrange(len(nodes1)):
make_link(G,nodes1[i],nodes2[i])
returnG
#graphexample
nodes=range(16)
graph=make_graph(nodes)
draw_graph3d(graph)

https://www.udacity.com/wiki/creating-network-graphs-with-python#Overview

7/8

6/27/2014

Creating Network Graphs with Python - Udacity

p 2014/04/25 08:51:02.

FM

MM

ff

xp

Mp

Jb

& M

MB PP

p FQ
Fb Pm

2011-2014 , .

https://www.udacity.com/wiki/creating-network-graphs-with-python#Overview

8/8

You might also like