graph_network.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """Visualize a 2D/3D network and its properties"""
  2. # (see also example: lineage_graph.py)
  3. from vedo import Points, show, sin
  4. from vedo.pyplot import DirectedGraph
  5. # Create some graph with nodes and edges
  6. # layouts: [2d, fast2d, clustering2d, circular, circular3d, cone, force, tree]
  7. g = DirectedGraph(layout='fast2d')
  8. ##################### Use networkx to create random nodes and edges
  9. # import networkx
  10. # G = networkx.gnm_random_graph(n=20, m=35)
  11. # for i, j in G.edges(): g.add_edge(j,i)
  12. ##################### Manually create nodes and edges
  13. for i in range(6): g.add_child(i) # add one child node to node i
  14. for i in range(3): g.add_child(i)
  15. for i in range(3): g.add_child(i)
  16. for i in range(7,9): g.add_child(i)
  17. for i in range(3): g.add_child(12) # add 3 children to node 12
  18. g.add_edge(1,16)
  19. ##################### build and draw
  20. g.build()
  21. graph = g[0].linewidth(4) # get the vedo 3d graph lines
  22. nodes = graph.vertices # get the 3d points of the nodes
  23. pts = Points(nodes, r=10).lighting('off')
  24. v1 = ['node'+str(n) for n in range(len(nodes))]
  25. v2 = [sin(x) for x in range(len(nodes))]
  26. labs1 = pts.labels(v1, scale=.02, italic=True).shift(.05,0.02,0).c('green')
  27. labs2 = pts.labels(v2, scale=.02, precision=3).shift(.05,-.02,0).c('red')
  28. # Interpolate the node value to color the edges:
  29. graph.cmap('viridis', v2).add_scalarbar3d(c='k')
  30. graph.scalarbar.shift(0.15,0,0).use_bounds(True)
  31. pts.cmap('viridis', v2)
  32. # This would colorize the edges directly with solid color based on a v3 array:
  33. # v3 = [sin(x) for x in range(graph.ncells)]
  34. # graph.cmap('jet', v3).add_scalarbar()
  35. show(pts, graph, labs1, labs2, __doc__, zoom='tight').close()