NetworkX系列教程(8)-Drawing Graph
如果只是简单使用nx.draw
,是无法定制出自己需要的graph,并且这样的graph内的点坐标的不定的,运行一次变一次,实际中一般是要求固定的位置,这就需要到布局
的概念了.详细的画图信息可以看这里,代码中的关键部分使用了英文进行注释,不在另外注释.
目录:
注意:如果代码出现找不库,请返回第一个教程,把库文件导入.
9.Drawing Graph
9.1使用Matplotlib
- #定义graph
- nodes=[0,1,2,3,4,5,'a','b','c']
- edges=[(0,1),(0,5),(1,2),(1,4),(2,1),(2,4),('a','b'),('b','c'),('c','a')]
- G=nx.Graph()
- G.add_nodes_from(nodes)
- G.add_edges_from(edges)
- #使用spring_layout布局
- pos=nx.spring_layout(G)
- plt.subplots(2,4,figsize=(18,6))
- plt.subplot(241)
- plt.title('spring_layout')
- nx.draw(G, with_labels=True, font_weight='bold') #Draw the graph G with Matplotlib.
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.subplot(242)
- plt.title('draw_networkx')
- nx.draw_networkx(G) #Draw the graph G using Matplotlib.
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.subplot(243)
- plt.title('draw_networkx_nodes')
- nx.draw_networkx_nodes(G,pos) #Draw the nodes of the graph G.
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.subplot(244)
- plt.title('draw_networkx_edges')
- nx.draw_networkx_edges(G,pos) #Draw the edges of the graph G.
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.subplot(245)
- plt.title('draw_networkx_labels')
- nx.draw_networkx_labels(G,pos) #Draw node labels on the graph G.
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.subplot(246)
- plt.title('draw_networkx_edge_labels')
- nx.draw_networkx_edge_labels(G,pos) #Draw edge labels.
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.subplot(247)
- plt.title('draw_circular')
- nx.draw_circular(G,) #Draw the graph G with a circular layout.
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.subplot(248)
- plt.title('draw_kamada_kawai')
- nx.draw_kamada_kawai(G) #Draw the graph G with a Kamada-Kawai force-directed layout.
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.show()
- plt.close()
- plt.subplots(1,4,figsize=(18,3))
- plt.subplot(141)
- plt.title('draw_random')
- nx.draw_random(G) #Draw the graph G with a random layout.
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.subplot(142)
- plt.title('draw_spectral')
- nx.draw_spectral(G,) #Draw the graph G with a spectral layout.
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.subplot(143)
- plt.title('draw_spring')
- nx.draw_spring(G) #Draw the graph G with a spring layout.
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.subplot(144)
- plt.title('draw_shell')
- nx.draw_shell(G) #Draw networkx graph with shell layout.
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.show()


9.2使用Graphviz AGraph (dot)
有些同学不知道如何安装Graphviz,我在这里作一个说明:
1.linux是安装graphviz即可,我使用的命令是:
- sudo apt install graphviz
2.Windows我没用实践过,不过我查到Graphviz有官网,里面有windows安装包,地址看下:
http://www.graphviz.org/download/
- G.clear()
- from networkx.drawing.nx_pydot import write_dot,read_dot
- plt.subplots(1,3,figsize=(15,5))
- K5 = nx.complete_graph(5)
- A = nx.nx_agraph.to_agraph(K5) #Return a pygraphviz graph from a NetworkX graph N.
- G1 = nx.nx_agraph.from_agraph(A) #Return a NetworkX Graph or DiGraph from a PyGraphviz graph.
- plt.subplot(131)
- plt.title('原图',fontproperties=myfont)
- nx.draw_random(G1) #Draw the graph G with a random layout.
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- write_dot(G1, 'graph.test') #Write NetworkX graph G to Graphviz dot format on path.
- G2=read_dot('graph.test') #Return a NetworkX graph from a dot file on path.
- plt.subplot(132)
- plt.title('保存原图后并读取',fontproperties=myfont)
- nx.draw_random(G2) #Draw the graph G with a random layout.
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- G3 = nx.petersen_graph()
- pos = nx.nx_agraph.graphviz_layout(G3) #Create node positions for G using Graphviz.
- plt.subplot(133)
- plt.title('graphviz_layout',fontproperties=myfont)
- nx.draw_random(G3) #Draw the graph G with a random layout.
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.show()

9.3图布局
- #定义graph
- nodes=[0,1,2,3,4,5,'a','b','c']
- edges=[(0,1),(0,5),(1,2),(1,4),(2,1),(2,4),('a','b'),('b','c'),('c','a')]
- G=nx.Graph()
- G.add_nodes_from(nodes)
- G.add_edges_from(edges)
- plt.subplots(2,3,figsize=(18,6))
- plt.subplot(231)
- plt.title('circular_layout')
- pos=nx.circular_layout(G) #Position nodes on a circle.
- nx.draw(G,pos, with_labels=True, font_weight='bold')
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.subplot(232)
- plt.title('kamada_kawai_layout')
- pos=nx.kamada_kawai_layout(G) #Position nodes using Kamada-Kawai path-length cost-function.
- nx.draw(G, pos,with_labels=True, font_weight='bold')
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.subplot(233)
- plt.title('random_layout')
- pos=nx.random_layout(G) #Position nodes uniformly at random in the unit square.
- nx.draw(G, pos,with_labels=True, font_weight='bold')
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.subplot(234)
- plt.title('shell_layout')
- pos=nx.shell_layout(G) #Position nodes in concentric circles.
- nx.draw(G, pos,with_labels=True, font_weight='bold')
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.subplot(235)
- plt.title('spring_layout')
- pos=nx.spring_layout(G)#Position nodes using Fruchterman-Reingold force-directed algorithm.
- nx.draw(G, pos, with_labels=True, font_weight='bold')
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.subplot(236)
- plt.title('spectral_layout')
- pos=nx.spectral_layout(G) #Position nodes using the eigenvectors of the graph Laplacian.
- nx.draw(G, pos, with_labels=True, font_weight='bold')
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.show()

NetworkX系列教程(8)-Drawing Graph的更多相关文章
- NetworkX系列教程(1)-创建graph
小书匠Graph图论 研究中经常涉及到图论的相关知识,而且常常面对某些术语时,根本不知道在说什么.前不久接触了NetworkX这个graph处理工具,发现这个工具已经解决绝大部分的图论问题(也许只是我 ...
- NetworkX系列教程(7)-对graph进行分析
小书匠Graph图论 graph构建完成后,对graph的连通等属性进行分析. 目录: 8.对图进行分析 8.1连通子图 8.2弱联通 8.3强连通 8.4子图 8.5条件过滤 注意:如果代码出现找不 ...
- NetworkX系列教程(6)-对graph进行操作
小书匠Graph图论 graph生成后,除了有查看操作,还有移除等操作,还有其他更多操作,具体可以看这里.下面将比较graph操作前后的不同. 目录: 7.对图进行操作 7.1移除某些节点和边 7.2 ...
- NetworkX系列教程(5)-查看graph的信息
小书匠Graph图论 有时候graph建好后,我们并不清除该graph内节点的,边的信息,这就需要调用函数去查看了. 目录: 6.查看Graph的信息 6.1查看graph内节点,边的 6.2查看gr ...
- NetworkX系列教程(4)-设置graph的信息
小书匠Graph图论 要画出美观的graph,需要对graph里面的节点,边,节点的布局都要进行设置,具体可以看官方文档:Adding attributes to graphs, nodes, and ...
- NetworkX系列教程(2)-graph生成器
小书匠Graph图论 本节主要讲解如何快速使用内置的方法生成graph,官方的文档在这里,里面包含了networkX的所有graph生成器,下面的内容只是我节选的内容,并将graph画出来而已. 声明 ...
- NetworkX系列教程(11)-graph和其他数据格式转换
小书匠 Graph 图论 学过线性代数的都了解矩阵,在矩阵上的文章可做的很多,什么特征矩阵,单位矩阵等.grpah存储可以使用矩阵,比如graph的邻接矩阵,权重矩阵等,这节主要是在等到graph后 ...
- NetworkX系列教程(3)-手动创建graph
小书匠Graph图论 不可否认,日常中我们使用最多的还是,使用自己的数据去手动创建自己的图形,而不是使用生成器,现从给graph添加点和边入手,讲解手动创建graph. 目录: 3.给graph添加节 ...
- NetworkX系列教程(10)-算法之五:广度优先与深度优先
小书匠Graph图论 重头戏部分来了,写到这里我感觉得仔细认真点了,可能在NetworkX中,实现某些算法就一句话的事,但是这个算法是做什么的,用在什么地方,原理是怎么样的,不清除,所以,我决定先把图 ...
随机推荐
- PB笔记之导入、导出组件
导入组件 导出组件
- mybatis执行DDL语句
对MyBatis一直停留在仅仅会用的阶段,常用的场景就是通过MyBatis对表数据进行DML(insert, delete, update等)操作,从来没有想过通过MyBatis对数据库进行DDL(c ...
- 怎样输出Hello World
方法一: 进入python交互模式, 然后使用: print()函数输出 方法二: 新建一个.py文件, 然后写入print()函数, 再使用python命令执行输出:
- hdu 3265 第一类斯特林数
先和第二类做一个对比 第一类Stirling数是有正负的,其绝对值是包含n个元素的集合分作k个环排列的方法数目.递推公式为, S(n,0) = 0, S(1,1) = 1. S(n+1,k) = S( ...
- 在论坛中出现的比较难的sql问题:11(字符分拆 多关键字匹配问题)
原文:在论坛中出现的比较难的sql问题:11(字符分拆 多关键字匹配问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉 ...
- interface Part2(定义接口)
一. 在 C# 语言中,类之间的继承关系仅支持单重继承,而接口是为了实现多重继承关系设计的. 二. 一个类能同时实现多个接口,还能在实现接口的同时再继承其他类,并且接口之间也可以继承. 三. 无论是表 ...
- Ajax调用WebService接口样例
在做手机端h5的应用时,通过Ajax调用http接口时没啥问题的:但有些老的接口是用WebService实现的,也来不及改成http的方式,这时通过Ajax调用会有些麻烦,在此记录具体实现过程.本文使 ...
- VBA For Each循环
For Each循环用于为数组或集合中的每个元素执行语句或一组语句.For Each循环与For循环类似; 然而,For Each循环是为数组或组中的每个元素执行的. 因此,这种类型的循环中将不存在步 ...
- QSqlDatabase数据库
#include <QSqlDatabase> #include <QtDebug> #include <QSqlQuery> #include <QSqlE ...
- Hive笔记及配置
Hive 基于Hadoop的数据仓库工具: 将结构化的数据文件,映射为一张表,并提供类SQL查询功能: 本质:将HQL转化为MapReduce程序: Hive处理的数据存储在HDFS: Hive分析数 ...