zoukankan      html  css  js  c++  java
  • NetworkX系列教程(6)-对graph进行操作

    graph生成后,除了有查看操作,还有移除等操作,还有其他更多操作,具体可以看这里.下面将比较graph操作前后的不同.

    目录:


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

    7.对图进行操作

    7.1移除某些节点和边

    1. #G.clear() 
    2. #生成graph 
    3. G=nx.path_graph(8) 
    4.  
    5. plt.subplots(1,2,figsize=(15,5)) 
    6. plt.suptitle('移除部分节点和边',fontproperties=myfont) 
    7.  
    8. #画出未操作前的graph 
    9. plt.subplot(121) 
    10. nx.draw(G, with_labels=True, font_weight='bold') 
    11. plt.title('操作前',fontproperties=myfont) 
    12. plt.axis('on') 
    13. plt.xticks([]) 
    14. plt.yticks([]) 
    15.  
    16. #移除部分节点和边,移除所有的点和边使用G.clear(),不再单独测试 
    17. G.remove_node(2) 
    18. G.remove_nodes_from([1,5]) 
    19. G.remove_edge(3, 4) 
    20.  
    21. #画出操作后的graph 
    22. plt.subplot(122) 
    23. nx.draw(G, with_labels=True, font_weight='bold') 
    24. plt.title('操作后',fontproperties=myfont) 
    25. plt.axis('on') 
    26. plt.xticks([]) 
    27. plt.yticks([]) 
    28.  
    29. #显示graph 
    30. plt.show() 

    png
    移除某些节点和边

    7.2合并graph

    1. # G1.clear() 
    2. # G2.clear() 
    3. # G3.clear() 
    4.  
    5. plt.subplots(1,3,figsize=(15,5)) 
    6. plt.suptitle('合并两个图',fontproperties=myfont) 
    7.  
    8. #生成graph1 
    9. G1=nx.path_graph(8) 
    10. plt.subplot(131) 
    11. nx.draw(G1, with_labels=True, font_weight='bold') 
    12. plt.title('图1',fontproperties=myfont) 
    13. plt.axis('on') 
    14. plt.xticks([]) 
    15. plt.yticks([]) 
    16.  
    17. #生成graph2 
    18. G2=nx.complete_graph(3) 
    19. plt.subplot(132) 
    20. nx.draw(G2, with_labels=True, font_weight='bold') 
    21. plt.title('图2',fontproperties=myfont) 
    22. plt.axis('on') 
    23. plt.xticks([]) 
    24. plt.yticks([]) 
    25.  
    26. #移除部分节点和边,移除所有的点和边使用G.clear(),不再单独测试 
    27. G3=nx.disjoint_union(G1,G2) 
    28. plt.subplot(133) 
    29. nx.draw(G3, with_labels=True, font_weight='bold') 
    30. plt.title('合并后',fontproperties=myfont) 
    31. plt.axis('on') 
    32. plt.xticks([]) 
    33. plt.yticks([]) 
    34.  
    35. #显示graph 
    36. plt.show() 

    png
    合并graph

    7.3有向图和无向图的转化

    1. #无向图转有向图 
    2. plt.subplots(1,2,figsize=(15,3)) 
    3. plt.suptitle('无向图转换为有向图',fontproperties=myfont) 
    4.  
    5. #定义无向图 
    6. G = nx.path_graph(8) 
    7. #转换为有向图 
    8. G2=G.to_directed() 
    9.  
    10. #下面是可视化转换前后的两个图 
    11. plt.subplot(121) 
    12. nx.draw(G, with_labels=True, font_weight='bold') 
    13. plt.title('无向图',fontproperties=myfont) 
    14. plt.axis('on') 
    15. plt.xticks([]) 
    16. plt.yticks([]) 
    17.  
    18. plt.subplot(122) 
    19. nx.draw(G2, with_labels=True, font_weight='bold') 
    20. plt.title('有向图',fontproperties=myfont) 
    21. plt.axis('on') 
    22. plt.xticks([]) 
    23. plt.yticks([]) 
    24. plt.show() 
    25. plt.close() 
    26.  
    27. #有向图转无向图 
    28. G.clear() 
    29. G2.clear() 
    30. plt.subplots(1,2,figsize=(15,3)) 
    31. plt.suptitle('有向图转换为无向图',fontproperties=myfont) 
    32.  
    33. #定义有向图 
    34. G = nx.path_graph(8,create_using=nx.DiGraph()) 
    35. #转换为无向图 
    36. G2=G.to_undirected() 
    37.  
    38. #下面是可视化转换前后的两个图 
    39. plt.subplot(121) 
    40. nx.draw(G, with_labels=True, font_weight='bold') 
    41. plt.title('有向图',fontproperties=myfont) 
    42. plt.axis('on') 
    43. plt.xticks([]) 
    44. plt.yticks([]) 
    45.  
    46. plt.subplot(122) 
    47. nx.draw(G2, with_labels=True, font_weight='bold') 
    48. plt.title('无向图',fontproperties=myfont) 
    49. plt.axis('on') 
    50. plt.xticks([]) 
    51. plt.yticks([]) 
    52. plt.show() 
    53.  
    54. # 注:可以看出无向图转有向图时,得到的边都是双向 

    png
    无向图转换为有向图

    png
    有向图转换为无向图

  • 相关阅读:
    vbr mp3
    NDK setup error
    转载 MPEG2视频解码在ARM11上的优化
    arm程序设计优化
    小情歌
    android update project
    Linux环境下的DNW使用
    2010的计划
    Setting up UDEV rules to grant access to your phone
    Debugging Native Code for android
  • 原文地址:https://www.cnblogs.com/wushaogui/p/9204243.html
Copyright © 2011-2022 走看看