小书匠Graph图论

本节主要讲解如何快速使用内置的方法生成graph,官方的文档在这里,里面包含了networkX的所有graph生成器,下面的内容只是我节选的内容,并将graph画出来而已.

声明,文中重复使用了以下代码块 ,现在统一注释在这里:

  1. plt.subplot(221) #生成2*2的组图,并且当前子图在2*2矩阵的第一个位置.第二个位置是222 

  2. plt.title('complete_graph') #子图的标题 

  3. nx.draw(G, with_labels=True, font_weight='bold') #将graph画出来 

  4. plt.axis('on') #需要坐标轴,以便框住graph 

  5. plt.xticks([]) #横坐标不需要刻度 

  6. plt.yticks([]) #纵坐标不需要刻度 

目录:


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

2.生成graph

2.1小图图集的生成器

  1. #graph_atlas的图已经被定义,只需要按标号取出来就可以,下面将前10个取出来 

  2. plt.subplots(2,5,figsize=(15,6)) 

  3. for ind in range(10): 

  4. G.clear() 


  5. G=nx.graph_atlas(ind) 

  6. plt.subplot(2,5,ind+1) 

  7. nx.draw(G,with_labels=True) 


  8. #下面是设置图片 

  9. plt.axis('on') 

  10. plt.title('graph_atlas_%s'%ind) 

  11. plt.xticks([]) 

  12. plt.yticks([]) 

  13. plt.show() 

  14. plt.close() 


小图图集的生成器

2.2调用函数生成经典的graph

  1. plt.subplots(2,2,figsize=(15,6)) 


  2. K_5 = nx.complete_graph(5) 

  3. plt.subplot(221) 

  4. plt.title('complete_graph') 

  5. nx.draw(K_5, with_labels=True, font_weight='bold') 

  6. plt.axis('on') 

  7. plt.xticks([]) 

  8. plt.yticks([]) 


  9. K_3_5 = nx.complete_bipartite_graph(3, 5) 

  10. plt.subplot(222) 

  11. plt.title('complete_bipartite_graph') 

  12. nx.draw(K_3_5, with_labels=True, font_weight='bold') 

  13. plt.axis('on') 

  14. plt.xticks([]) 

  15. plt.yticks([]) 


  16. barbell = nx.barbell_graph(10, 10) 

  17. plt.subplot(223) 

  18. plt.title('barbell_graph') 

  19. nx.draw(barbell, with_labels=True, font_weight='bold') 

  20. plt.axis('on') 

  21. plt.xticks([]) 

  22. plt.yticks([]) 


  23. lollipop = nx.lollipop_graph(10, 20) 

  24. plt.subplot(224) 

  25. plt.title('lollipop_graph') 

  26. nx.draw(lollipop, with_labels=True, font_weight='bold') 

  27. plt.axis('on') 

  28. plt.xticks([]) 

  29. plt.yticks([]) 


  30. plt.show() 


经典的graph

2.3格子graph

  1. G.clear() 


  2. plt.subplots(2,3,figsize=(15,6)) 

  3. #二维网格图 

  4. G=nx.grid_2d_graph(2,3) 

  5. plt.subplot(2,3,1) 

  6. nx.draw(G,with_labels=True) 

  7. plt.title('grid_2d_graph') 

  8. plt.axis('on') 

  9. plt.xticks([]) 

  10. plt.yticks([]) 


  11. #n维网格图 

  12. grid_graph = nx.grid_graph(dim=[1, 3, 4]) 

  13. plt.subplot(2,3,2) 

  14. nx.draw(grid_graph,with_labels=True) 

  15. plt.title('grid_graph') 

  16. plt.axis('on') 

  17. plt.xticks([]) 

  18. plt.yticks([]) 


  19. #m×n的六角形格子图。 

  20. G=nx.hexagonal_lattice_graph(2,3) 

  21. plt.subplot(2,3,3) 

  22. nx.draw(G,with_labels=True) 

  23. plt.title('hexagonal_lattice_graph') 

  24. plt.axis('on') 

  25. plt.xticks([]) 

  26. plt.yticks([]) 


  27. #n维超立方体图形。 

  28. G=nx.hypercube_graph(3) 

  29. plt.subplot(2,3,4) 

  30. nx.draw(G,with_labels=True) 

  31. plt.title('hypercube_graph') 

  32. plt.axis('on') 

  33. plt.xticks([]) 

  34. plt.yticks([]) 


  35. #三角格子图 

  36. G=nx.triangular_lattice_graph(1,3) 

  37. plt.subplot(2,3,5) 

  38. nx.draw(G,with_labels=True) 

  39. plt.title('hypercube_graph') 

  40. plt.axis('on') 

  41. plt.xticks([]) 

  42. plt.yticks([]) 


  43. plt.show() 


格子graph

2.4各种已经被命名的小graph

  1. plt.subplots(2,2,figsize=(15,6)) 


  2. petersen = nx.petersen_graph() 

  3. plt.subplot(221) 

  4. plt.title('petersen_graph') 

  5. nx.draw(petersen, with_labels=True, font_weight='bold') 

  6. plt.axis('on') 

  7. plt.xticks([]) 

  8. plt.yticks([]) 


  9. tutte = nx.tutte_graph() 

  10. plt.subplot(222) 

  11. plt.title('tutte_graph') 

  12. nx.draw(tutte, with_labels=True, font_weight='bold') 

  13. plt.axis('on') 

  14. plt.xticks([]) 

  15. plt.yticks([]) 


  16. maze = nx.sedgewick_maze_graph() 

  17. plt.subplot(223) 

  18. plt.title('sedgewick_maze_graph') 

  19. nx.draw(maze, with_labels=True, font_weight='bold') 

  20. plt.axis('on') 

  21. plt.xticks([]) 

  22. plt.yticks([]) 


  23. tet = nx.tetrahedral_graph() 

  24. plt.subplot(224) 

  25. plt.title('tetrahedral_graph') 

  26. nx.draw(tet, with_labels=True, font_weight='bold') 

  27. plt.axis('on') 

  28. plt.xticks([]) 

  29. plt.yticks([]) 


  30. plt.show() 


已经被命名的小graph

2.5使用随机graph生成器

  1. plt.subplots(2,2,figsize=(15,6)) 


  2. er = nx.erdos_renyi_graph(10, 0.15) 

  3. plt.subplot(221) 

  4. plt.title('erdos_renyi_graph') 

  5. nx.draw(er, with_labels=True, font_weight='bold') 

  6. plt.axis('on') 

  7. plt.xticks([]) 

  8. plt.yticks([]) 


  9. ws = nx.watts_strogatz_graph(30, 3, 0.1) 

  10. plt.subplot(222) 

  11. plt.title('watts_strogatz_graph') 

  12. nx.draw(ws, with_labels=True, font_weight='bold') 

  13. plt.axis('on') 

  14. plt.xticks([]) 

  15. plt.yticks([]) 


  16. ba = nx.barabasi_albert_graph(10, 5) 

  17. plt.subplot(223) 

  18. plt.title('barabasi_albert_graph') 

  19. nx.draw(ba, with_labels=True, font_weight='bold') 

  20. plt.axis('on') 

  21. plt.xticks([]) 

  22. plt.yticks([]) 


  23. red = nx.random_lobster(10, 0.9, 0.9) 

  24. plt.subplot(224) 

  25. plt.title('random_lobster') 

  26. nx.draw(red, with_labels=True, font_weight='bold') 

  27. plt.axis('on') 

  28. plt.xticks([]) 

  29. plt.yticks([]) 


  30. plt.show() 


随机graph生成器

2.6社交网络

  1. plt.subplots(2,2,figsize=(15,6)) 


  2. #返回Zachary的空手道俱乐部图。 

  3. G.clear() 

  4. G = nx.karate_club_graph() 

  5. plt.subplot(1,2,1) 

  6. nx.draw(G,with_labels=True) 

  7. plt.title('karate_club_graph') 

  8. plt.axis('on') 

  9. plt.xticks([]) 

  10. plt.yticks([]) 


  11. #戴维斯南方女性社交网络。 

  12. G.clear() 

  13. G = nx.davis_southern_women_graph() 

  14. plt.subplot(1,2,2) 

  15. nx.draw(G,with_labels=True) 

  16. plt.title('davis_southern_women_graph') 

  17. plt.axis('on') 

  18. plt.xticks([]) 

  19. plt.yticks([]) 


  20. plt.show() 


社交网络

2.7社区

  1. plt.subplots(2,2,figsize=(15,6)) 


  2. er = nx.caveman_graph(3,3) 

  3. plt.subplot(221) 

  4. plt.title('caveman_graph') 

  5. nx.draw(er, with_labels=True, font_weight='bold') 

  6. plt.axis('on') 

  7. plt.xticks([]) 

  8. plt.yticks([]) 


  9. ws = nx.random_partition_graph([10,10,10],.25,.01) 

  10. plt.subplot(222) 

  11. plt.title('random_partition_graph') 

  12. nx.draw(ws, with_labels=True, font_weight='bold') 

  13. plt.axis('on') 

  14. plt.xticks([]) 

  15. plt.yticks([]) 


  16. ba = nx.ring_of_cliques(8, 4) 

  17. plt.subplot(223) 

  18. plt.title('ring_of_cliques') 

  19. nx.draw(ba, with_labels=True, font_weight='bold') 

  20. plt.axis('on') 

  21. plt.xticks([]) 

  22. plt.yticks([]) 


  23. red = nx.windmill_graph(4,5) 

  24. plt.subplot(224) 

  25. plt.title('windmill_graph') 

  26. nx.draw(red, with_labels=True, font_weight='bold') 

  27. plt.axis('on') 

  28. plt.xticks([]) 

  29. plt.yticks([]) 


  30. plt.show() 


社区

2.8树

  1. #返回随机树 

  2. G.clear() 

  3. G = nx.random_tree(10) 

  4. nx.draw(G,with_labels=True) 

  5. plt.title('random_tree') 

  6. plt.axis('on') 

  7. plt.xticks([]) 

  8. plt.yticks([]) 


  9. plt.show() 


NetworkX系列教程(2)-graph生成器的更多相关文章

  1. NetworkX系列教程(11)-graph和其他数据格式转换

    小书匠 Graph 图论  学过线性代数的都了解矩阵,在矩阵上的文章可做的很多,什么特征矩阵,单位矩阵等.grpah存储可以使用矩阵,比如graph的邻接矩阵,权重矩阵等,这节主要是在等到graph后 ...

  2. NetworkX系列教程(1)-创建graph

    小书匠Graph图论 研究中经常涉及到图论的相关知识,而且常常面对某些术语时,根本不知道在说什么.前不久接触了NetworkX这个graph处理工具,发现这个工具已经解决绝大部分的图论问题(也许只是我 ...

  3. NetworkX系列教程(8)-Drawing Graph

    小书匠Graph图论 如果只是简单使用nx.draw,是无法定制出自己需要的graph,并且这样的graph内的点坐标的不定的,运行一次变一次,实际中一般是要求固定的位置,这就需要到布局的概念了.详细 ...

  4. NetworkX系列教程(3)-手动创建graph

    小书匠Graph图论 不可否认,日常中我们使用最多的还是,使用自己的数据去手动创建自己的图形,而不是使用生成器,现从给graph添加点和边入手,讲解手动创建graph. 目录: 3.给graph添加节 ...

  5. NetworkX系列教程(7)-对graph进行分析

    小书匠Graph图论 graph构建完成后,对graph的连通等属性进行分析. 目录: 8.对图进行分析 8.1连通子图 8.2弱联通 8.3强连通 8.4子图 8.5条件过滤 注意:如果代码出现找不 ...

  6. NetworkX系列教程(6)-对graph进行操作

    小书匠Graph图论 graph生成后,除了有查看操作,还有移除等操作,还有其他更多操作,具体可以看这里.下面将比较graph操作前后的不同. 目录: 7.对图进行操作 7.1移除某些节点和边 7.2 ...

  7. NetworkX系列教程(5)-查看graph的信息

    小书匠Graph图论 有时候graph建好后,我们并不清除该graph内节点的,边的信息,这就需要调用函数去查看了. 目录: 6.查看Graph的信息 6.1查看graph内节点,边的 6.2查看gr ...

  8. NetworkX系列教程(4)-设置graph的信息

    小书匠Graph图论 要画出美观的graph,需要对graph里面的节点,边,节点的布局都要进行设置,具体可以看官方文档:Adding attributes to graphs, nodes, and ...

  9. NetworkX系列教程(10)-算法之五:广度优先与深度优先

    小书匠Graph图论 重头戏部分来了,写到这里我感觉得仔细认真点了,可能在NetworkX中,实现某些算法就一句话的事,但是这个算法是做什么的,用在什么地方,原理是怎么样的,不清除,所以,我决定先把图 ...

随机推荐

  1. redis集合数据类型---SET

    一.概述 redis的set是string类型的无序集合 集合成员是唯一的,这就意味着集合中不能出现重复的数据. 集合中最大的成员数为2^32-1(4294967295,每个集合可存储40多亿个成员) ...

  2. 怎样检测浏览器是否安装了某个插件, 比如flash

    首先, 我们可以获取浏览器安装的所有在插件: navigator.plugins 它会返回一个类似数组的对象, 包含所有已安装插件的具体信息. navigator.plugins; 然后我们可以通过正 ...

  3. hdu 1548 简单bfs。。。

    由于题目过水.. 我就在这里把bfs的模板写一些吧.. bfs的思想是利用队列的特性 对树的每一层先遍历 每一次访问时取出队首 然后排出~ #include<queue>void bfs( ...

  4. Linux修改主机名方法

    [root@lyx ~]# vim /etc/hosts   vim代表修改,进入hosts文件进行添加192.168.10.128 hadoop128 [root@lyx ~]# hostname ...

  5. javascript 之 扩展对象 jQuery.extend

    在JQuery的API手册中,extend方法挂载在JQuery 和 JQuery.fn两个不同的对象上,但在JQuery内部代码实现的是相同的,只是功能各不相同. 官方解释: jQuery.exte ...

  6. webstrom设置语句中的分号

    webstrom可以设置语句默认是否添加分号 setting >editor > Code Style > Javascript

  7. java SE,EE,ME区别

    Java SE(Java Platform,Standard Edition):java平台标准版: Java EE(Java Platform.Enterprise Edition):java平台企 ...

  8. 我们为什么要用redis

    Redis的5要点: 1.为什么要选择Redis:介绍Redis的使用场景与使用Redis的原因: 2.Redis常用命令总结:包括时间复杂度总结与具体数据类型在Redis内部使用的数据结构: 3.R ...

  9. .net core的startup中配置读取

    public class Startup { public Startup(IHostingEnvironment env) { Configuration = new ConfigurationBu ...

  10. Scrapy 安装与使用

    Scrapy的安装: 当前环境win10,python_3.6.4,64bit.在命令提示符窗口运行pip install Scrapy,出现以下结果: building 'twisted.test. ...