mayavi2_spring.py 934 B

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