Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / examples / 3d_drawing / mayavi2_spring.py @ 5cef0f13

History | View | Annotate | Download (1.12 KB)

1
"""
2
=======
3
Mayavi2
4
=======
5

6
This is
7
"""
8

    
9
# needs mayavi2
10
# run with ipython -wthread
11
import networkx as nx
12
import numpy as np
13
from mayavi import mlab
14
mlab.options.offscreen = True
15

    
16
# some graphs to try
17
# H=nx.krackhardt_kite_graph()
18
# H=nx.Graph();H.add_edge('a','b');H.add_edge('a','c');H.add_edge('a','d')
19
# H=nx.grid_2d_graph(4,5)
20
H = nx.cycle_graph(20)
21

    
22
# reorder nodes from 0,len(G)-1
23
G = nx.convert_node_labels_to_integers(H)
24
# 3d spring layout
25
pos = nx.spring_layout(G, dim=3)
26
# numpy array of x,y,z positions in sorted node order
27
xyz = np.array([pos[v] for v in sorted(G)])
28
# scalar colors
29
scalars = np.array(list(G.nodes())) + 5
30

    
31
mlab.figure(1, bgcolor=(0, 0, 0))
32
mlab.clf()
33

    
34
pts = mlab.points3d(xyz[:, 0], xyz[:, 1], xyz[:, 2],
35
                    scalars,
36
                    scale_factor=0.1,
37
                    scale_mode='none',
38
                    colormap='Blues',
39
                    resolution=20)
40

    
41
pts.mlab_source.dataset.lines = np.array(list(G.edges()))
42
tube = mlab.pipeline.tube(pts, tube_radius=0.01)
43
mlab.pipeline.surface(tube, color=(0.8, 0.8, 0.8))
44

    
45
mlab.savefig('mayavi2_spring.png')
46
# mlab.show() # interactive window