码迷,mamicode.com
首页 > Windows程序 > 详细

NetworkX系列教程(8)-Drawing Graph

时间:2018-06-20 22:36:54      阅读:699      评论:0      收藏:0      [点我收藏+]

标签:bee   any   tick   1.2   png   无法   raw   directed   shu   

如果只是简单使用nx.draw,是无法定制出自己需要的graph,并且这样的graph内的点坐标的不定的,运行一次变一次,实际中一般是要求固定的位置,这就需要到布局的概念了.详细的画图信息可以看这里,代码中的关键部分使用了英文进行注释,不在另外注释.

目录:


注意:如果代码出现找不库,请返回第一个教程,把库文件导入.

9.Drawing Graph

9.1使用Matplotlib

  1. #定义graph 
  2. nodes=[0,1,2,3,4,5,‘a‘,‘b‘,‘c‘
  3. edges=[(0,1),(0,5),(1,2),(1,4),(2,1),(2,4),(‘a‘,‘b‘),(‘b‘,‘c‘),(‘c‘,‘a‘)] 
  4. G=nx.Graph() 
  5. G.add_nodes_from(nodes) 
  6. G.add_edges_from(edges) 
  7.  
  8. #使用spring_layout布局 
  9. pos=nx.spring_layout(G) 
  10.  
  11. plt.subplots(2,4,figsize=(18,6)) 
  12. plt.subplot(241
  13. plt.title(‘spring_layout‘
  14. nx.draw(G, with_labels=True, font_weight=‘bold‘) #Draw the graph G with Matplotlib. 
  15. plt.axis(‘on‘
  16. plt.xticks([]) 
  17. plt.yticks([]) 
  18.  
  19. plt.subplot(242
  20. plt.title(‘draw_networkx‘
  21. nx.draw_networkx(G) #Draw the graph G using Matplotlib. 
  22. plt.axis(‘on‘
  23. plt.xticks([]) 
  24. plt.yticks([]) 
  25.  
  26. plt.subplot(243
  27. plt.title(‘draw_networkx_nodes‘
  28. nx.draw_networkx_nodes(G,pos) #Draw the nodes of the graph G. 
  29. plt.axis(‘on‘
  30. plt.xticks([]) 
  31. plt.yticks([]) 
  32.  
  33. plt.subplot(244
  34. plt.title(‘draw_networkx_edges‘
  35. nx.draw_networkx_edges(G,pos) #Draw the edges of the graph G. 
  36. plt.axis(‘on‘
  37. plt.xticks([]) 
  38. plt.yticks([]) 
  39.  
  40. plt.subplot(245
  41. plt.title(‘draw_networkx_labels‘
  42. nx.draw_networkx_labels(G,pos) #Draw node labels on the graph G. 
  43. plt.axis(‘on‘
  44. plt.xticks([]) 
  45. plt.yticks([]) 
  46.  
  47. plt.subplot(246
  48. plt.title(‘draw_networkx_edge_labels‘
  49. nx.draw_networkx_edge_labels(G,pos) #Draw edge labels. 
  50. plt.axis(‘on‘
  51. plt.xticks([]) 
  52. plt.yticks([]) 
  53.  
  54. plt.subplot(247
  55. plt.title(‘draw_circular‘
  56. nx.draw_circular(G,) #Draw the graph G with a circular layout. 
  57. plt.axis(‘on‘
  58. plt.xticks([]) 
  59. plt.yticks([]) 
  60.  
  61. plt.subplot(248
  62. plt.title(‘draw_kamada_kawai‘
  63. nx.draw_kamada_kawai(G) #Draw the graph G with a Kamada-Kawai force-directed layout. 
  64. plt.axis(‘on‘
  65. plt.xticks([]) 
  66. plt.yticks([]) 
  67. plt.show() 
  68. plt.close() 
  69.  
  70. plt.subplots(1,4,figsize=(18,3)) 
  71. plt.subplot(141
  72. plt.title(‘draw_random‘
  73. nx.draw_random(G) #Draw the graph G with a random layout. 
  74. plt.axis(‘on‘
  75. plt.xticks([]) 
  76. plt.yticks([]) 
  77.  
  78. plt.subplot(142
  79. plt.title(‘draw_spectral‘
  80. nx.draw_spectral(G,) #Draw the graph G with a spectral layout. 
  81. plt.axis(‘on‘
  82. plt.xticks([]) 
  83. plt.yticks([]) 
  84.  
  85. plt.subplot(143
  86. plt.title(‘draw_spring‘
  87. nx.draw_spring(G) #Draw the graph G with a spring layout. 
  88. plt.axis(‘on‘
  89. plt.xticks([]) 
  90. plt.yticks([]) 
  91.  
  92. plt.subplot(144
  93. plt.title(‘draw_shell‘
  94. nx.draw_shell(G) #Draw networkx graph with shell layout. 
  95. plt.axis(‘on‘
  96. plt.xticks([]) 
  97. plt.yticks([]) 
  98.  
  99. plt.show() 

技术分享图片
Matplotlib布局1

技术分享图片
Matplotlib布局2

9.2使用Graphviz AGraph (dot)

  1. G.clear() 
  2. from networkx.drawing.nx_pydot import write_dot,read_dot 
  3.  
  4. plt.subplots(1,3,figsize=(15,5)) 
  5. K5 = nx.complete_graph(5
  6.  
  7. A = nx.nx_agraph.to_agraph(K5) #Return a pygraphviz graph from a NetworkX graph N. 
  8. G1 = nx.nx_agraph.from_agraph(A) #Return a NetworkX Graph or DiGraph from a PyGraphviz graph. 
  9. plt.subplot(131
  10. plt.title(‘原图‘,fontproperties=myfont) 
  11. nx.draw_random(G1) #Draw the graph G with a random layout. 
  12. plt.axis(‘on‘
  13. plt.xticks([]) 
  14. plt.yticks([]) 
  15.  
  16. write_dot(G1, ‘graph.test‘) #Write NetworkX graph G to Graphviz dot format on path. 
  17. G2=read_dot(‘graph.test‘) #Return a NetworkX graph from a dot file on path. 
  18. plt.subplot(132
  19. plt.title(‘保存原图后并读取‘,fontproperties=myfont) 
  20. nx.draw_random(G2) #Draw the graph G with a random layout. 
  21. plt.axis(‘on‘
  22. plt.xticks([]) 
  23. plt.yticks([]) 
  24.  
  25. G3 = nx.petersen_graph() 
  26. pos = nx.nx_agraph.graphviz_layout(G3) #Create node positions for G using Graphviz. 
  27.  
  28. plt.subplot(133
  29. plt.title(‘graphviz_layout‘,fontproperties=myfont) 
  30. nx.draw_random(G3) #Draw the graph G with a random layout. 
  31. plt.axis(‘on‘
  32. plt.xticks([]) 
  33. plt.yticks([]) 
  34.  
  35. plt.show() 

技术分享图片
Graphviz画图

9.3图布局

  1. #定义graph 
  2. nodes=[0,1,2,3,4,5,‘a‘,‘b‘,‘c‘
  3. edges=[(0,1),(0,5),(1,2),(1,4),(2,1),(2,4),(‘a‘,‘b‘),(‘b‘,‘c‘),(‘c‘,‘a‘)] 
  4. G=nx.Graph() 
  5. G.add_nodes_from(nodes) 
  6. G.add_edges_from(edges) 
  7.  
  8. plt.subplots(2,3,figsize=(18,6)) 
  9. plt.subplot(231
  10. plt.title(‘circular_layout‘
  11. pos=nx.circular_layout(G) #Position nodes on a circle. 
  12. nx.draw(G,pos, with_labels=True, font_weight=‘bold‘
  13. plt.axis(‘on‘
  14. plt.xticks([]) 
  15. plt.yticks([]) 
  16.  
  17. plt.subplot(232
  18. plt.title(‘kamada_kawai_layout‘
  19. pos=nx.kamada_kawai_layout(G) #Position nodes using Kamada-Kawai path-length cost-function. 
  20. nx.draw(G, pos,with_labels=True, font_weight=‘bold‘
  21. plt.axis(‘on‘
  22. plt.xticks([]) 
  23. plt.yticks([]) 
  24.  
  25. plt.subplot(233
  26. plt.title(‘random_layout‘
  27. pos=nx.random_layout(G) #Position nodes uniformly at random in the unit square. 
  28. nx.draw(G, pos,with_labels=True, font_weight=‘bold‘
  29. plt.axis(‘on‘
  30. plt.xticks([]) 
  31. plt.yticks([]) 
  32.  
  33. plt.subplot(234
  34. plt.title(‘shell_layout‘
  35. pos=nx.shell_layout(G) #Position nodes in concentric circles. 
  36. nx.draw(G, pos,with_labels=True, font_weight=‘bold‘
  37. plt.axis(‘on‘
  38. plt.xticks([]) 
  39. plt.yticks([]) 
  40.  
  41. plt.subplot(235
  42. plt.title(‘spring_layout‘
  43. pos=nx.spring_layout(G)#Position nodes using Fruchterman-Reingold force-directed algorithm. 
  44. nx.draw(G, pos, with_labels=True, font_weight=‘bold‘
  45. plt.axis(‘on‘
  46. plt.xticks([]) 
  47. plt.yticks([]) 
  48.  
  49. plt.subplot(236
  50. plt.title(‘spectral_layout‘
  51. pos=nx.spectral_layout(G) #Position nodes using the eigenvectors of the graph Laplacian. 
  52. nx.draw(G, pos, with_labels=True, font_weight=‘bold‘
  53. plt.axis(‘on‘
  54. plt.xticks([]) 
  55. plt.yticks([]) 
  56.  
  57. plt.show() 

技术分享图片
图布局

NetworkX系列教程(8)-Drawing Graph

标签:bee   any   tick   1.2   png   无法   raw   directed   shu   

原文地址:https://www.cnblogs.com/wushaogui/p/9206144.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!