mayavi2_spring.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """
  2. =======
  3. Mayavi2
  4. =======
  5. This is
  6. """
  7. # needs mayavi2
  8. # run with ipython -wthread
  9. import networkx as nx
  10. import numpy as np
  11. from mayavi import mlab
  12. mlab.options.offscreen = True
  13. # some graphs to try
  14. # H=nx.krackhardt_kite_graph()
  15. # H=nx.Graph();H.add_edge('a','b');H.add_edge('a','c');H.add_edge('a','d')
  16. # H=nx.grid_2d_graph(4,5)
  17. H = nx.cycle_graph(20)
  18. # reorder nodes from 0,len(G)-1
  19. G = nx.convert_node_labels_to_integers(H)
  20. # 3d spring layout
  21. pos = nx.spring_layout(G, dim=3)
  22. # numpy array of x,y,z positions in sorted node order
  23. xyz = np.array([pos[v] for v in sorted(G)])
  24. # scalar colors
  25. scalars = np.array(list(G.nodes())) + 5
  26. mlab.figure(1, bgcolor=(0, 0, 0))
  27. mlab.clf()
  28. pts = mlab.points3d(xyz[:, 0], xyz[:, 1], xyz[:, 2],
  29. scalars,
  30. scale_factor=0.1,
  31. scale_mode='none',
  32. colormap='Blues',
  33. resolution=20)
  34. pts.mlab_source.dataset.lines = np.array(list(G.edges()))
  35. tube = mlab.pipeline.tube(pts, tube_radius=0.01)
  36. mlab.pipeline.surface(tube, color=(0.8, 0.8, 0.8))
  37. mlab.savefig('mayavi2_spring.png')
  38. # mlab.show() # interactive window