1/nx.draw()
该函数默认的节点布局算法是弹簧布局,及pos=nx.spring_layout(),
当然,我们可以自己指定使用哪种布局算法,如下图,我们使用的是circular_layout()布局算法
2/draw_networkx()
3/draw_networkx_nodes()
仅绘制图G的节点
4/draw_networkx_edges()
仅绘制图形G的边
5/draw_networkx_labels()
6/draw_networkx_edge_labels()
绘制网络图&&显示图&&保留图
G为我们想要绘出的图,pos为网络图布局,我试了很多布局发现nx.drawing.layout.spring_layout(G)通常是我们想要的布局
node_size: 指定节点的尺寸大小(默认是300,单位未知,就是上图中那么大的点)
node_color: 指定节点的颜色 (默认是红色,可以用字符串简单标识颜色,例如'r'为红色,'b'为绿色等,具体可查看手册),用“数据字典”赋值的时候必须对字典取值(.values())后再赋值
node_shape: 节点的形状(默认是圆形,用字符串'o'标识,具体可查看手册)
alpha: 透明度 (默认是1.0,不透明,0为完全透明)
width: 边的宽度 (默认为1.0)
edge_color: 边的颜色(默认为黑色)
style: 边的样式(默认为实线,可选: solid,dashed,dotted,dashdot)
with_labels: 节点是否带标签(默认为True)
font_size: 节点标签字体大小 (默认为12)
font_color: 节点标签字体颜色(默认为黑色)
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
colors = range(20)
G.add_nodes_from(nodes)
G.add_weighted_edges_from(edges)
nx.draw(G,
pos = nx.drawing.layout.spring_layout(G),
node_size = 0.5,
node_color="r",
width = 0.01,
edge_colo r= b",
edge_cmap=plt.cm.gray,
with_labels=False)
plt.savefig("/home/eff_server/cohesion.png")
plt.show()
建立布局,对图进行布局美化,networkx 提供的布局方式有:
nx.circular_layout:节点在一个圆环上均匀分布,pos = nx.circular_layout(G)
nx.random_layout:节点随机分布,pos = nx.random_layout(G)
nx.shell_layout:节点在同心圆上分布
nx.spring_layout: 用Fruchterman-Reingold算法排列节点(这个算法我不了解,样子类似多中心放射状)
nx.spectral_layout(G):根据图的拉普拉斯特征向量排列节
import networkx as nx
nx.draw(G,[pos,ax,hold])
nx.draw_networkx(G,[pos,with_labels])
nx.draw_networkx_nodes(G,pos,[nodelist]) 绘制网络G的节点图
nx.draw_networkx_edges(G,pos[edgelist]) 绘制网络G的边图
nx.draw_networkx_edge_labels(G, pos[, ...]) 绘制网络G的边图,边有label
---有layout 布局画图函数的分界线---
draw_circular(G, **kwargs) Draw the graph G with a circular layout.
draw_random(G, **kwargs) Draw the graph G with a random layout.
draw_spectral(G, **kwargs)Draw the graph G with a spectral layout.
draw_spring(G, **kwargs)Draw the graph G with a spring layout.
draw_shell(G, **kwargs) Draw networkx graph with shell layout.
draw_graphviz(G[, prog])Draw networkx graph with graphviz layout.
networkx 画图参数:
node_size: 指定节点的尺寸大小(默认是300,单位不知道,具体大小可以通过调试找到一个最合适的),可以人为设置不同的节点大小不同
node_color: 指定节点的颜色 (默认是红色,可以用字符串简单标识颜色,例如'r'为红色,'b'为绿色等),可以人为设置不同的节点颜色不同
node_shape: 节点的形状(默认是圆形,用字符串'o'标识)
alpha: 透明度 (默认是1.0,不透明,0为完全透明)
width: 边的宽度 (默认为1.0),可以人为设置边的宽度,,可以由权重来决定
edge_color: 边的颜色(默认为黑色)
style: 边的样式(默认为实线,可选: solid,dashed,dotted,dashdot)
with_labels: 节点是否带标签(默认为True),即是否显示节点的名字
font_size: 节点标签字体大小 (默认为12) ,节点名字字体的大小
font_color: 节点标签字体颜色(默认为黑色),,节点名字的颜色
pos(dictionary, optional): 节点的布局,即每个节点在网络的什么位置
布局指定节点排列形式
circular_layout:节点在一个圆环上均匀分布
random_layout:节点随机分布
shell_layout:节点在同心圆上分布
spring_layout: 用Fruchterman-Reingold算法排列节点,中心放射状
spectral_layout:根据图的拉普拉斯特征向量排列节点
布局也可用pos参数指定,例如,nx.draw(G, pos = spring_layout(G)) 这样指定了networkx上以中心放射状分布.
其中:spring_layout需要进行计算,拥有以下参数:
pos = nx.spring_layout(G,iterations=200)
k controls the distance between the nodes and varies between 0 and 1
iterations is the number of times simulated annealing is run
default k =0.1 and iterations=50
力引导布局(Force-directed Layout)
力引导布局最早由Peter Eades在1984年的“启发式画图算法”一文中提出,目的是减少布局中边的交叉,尽量保持边的长度一致。此方法借用弹簧模型模拟布局过程:用弹簧模拟两个点之间的关系,受到弹力的作用后,过近的点会被弹开而过远的点被拉近;通过不断的迭代,整个布局达到动态平衡,趋于稳定。其后,“力引导”的概念被提出,演化成力引导布局算法FR(Fruchterman-Reingold算法)——丰富两点之间的物理模型,加入点之间的静电力,通过计算系统的总能量并使得能量最小化,从而达到布局的目的。这种改进的能量模型,可看成弹簧模型的一般化。
参考:https://blog.csdn.net/gdp12315_gu/article/details/48351589
根据权重改变2节点之间线的粗细
import networkx as nx
G = nx.Graph()
G.add_edge(s,t,weight=w)
nx.draw(G,
pos = nx.spring_layout(G,iterations=200),
node_size = nodes_size_list,
node_color = nodes_colour_list,
width = [float(w['heat_value'] * 5) for (u,v,w) in G.edges(data=True)],
edge_color = "b",
edge_cmap = plt.get_cmap("viridis"),
with_labels = True,
font_weight = "bold")
import networkx as nx
nx.draw(G,[pos,ax,hold])
nx.draw_networkx(G,[pos,with_labels])
nx.draw_networkx_nodes(G,pos,[nodelist]) 绘制网络G的节点图
nx.draw_networkx_edges(G,pos[edgelist]) 绘制网络G的边图
nx.draw_networkx_edge_labels(G, pos[, ...]) 绘制网络G的边图,边有label
---有layout 布局画图函数的分界线---
draw_circular(G, **kwargs) Draw the graph G with a circular layout.
draw_random(G, **kwargs) Draw the graph G with a random layout.
draw_spectral(G, **kwargs)Draw the graph G with a spectral layout.
draw_spring(G, **kwargs)Draw the graph G with a spring layout.
draw_shell(G, **kwargs) Draw networkx graph with shell layout.
draw_graphviz(G[, prog])Draw networkx graph with graphviz layout.
networkx 画图参数:
node_size: 指定节点的尺寸大小(默认是300,单位不知道,具体大小可以通过调试找到一个最合适的),可以人为设置不同的节点大小不同
node_color: 指定节点的颜色 (默认是红色,可以用字符串简单标识颜色,例如'r'为红色,'b'为绿色等),可以人为设置不同的节点颜色不同
node_shape: 节点的形状(默认是圆形,用字符串'o'标识)
alpha: 透明度 (默认是1.0,不透明,0为完全透明)
width: 边的宽度 (默认为1.0),可以人为设置边的宽度,,可以由权重来决定
edge_color: 边的颜色(默认为黑色)
style: 边的样式(默认为实线,可选: solid,dashed,dotted,dashdot)
with_labels: 节点是否带标签(默认为True),即是否显示节点的名字
font_size: 节点标签字体大小 (默认为12) ,节点名字字体的大小
font_color: 节点标签字体颜色(默认为黑色),,节点名字的颜色
pos(dictionary, optional): 节点的布局,即每个节点在网络的什么位置
布局指定节点排列形式
circular_layout:节点在一个圆环上均匀分布
random_layout:节点随机分布
shell_layout:节点在同心圆上分布
spring_layout: 用Fruchterman-Reingold算法排列节点,中心放射状
spectral_layout:根据图的拉普拉斯特征向量排列节点
布局也可用pos参数指定,例如,nx.draw(G, pos = spring_layout(G)) 这样指定了networkx上以中心放射状分布.
其中:spring_layout需要进行计算,拥有以下参数:
pos = nx.spring_layout(G,iterations=200)
k controls the distance between the nodes and varies between 0 and 1
iterations is the number of times simulated annealing is run
default k =0.1 and iterations=50
力引导布局(Force-directed Layout)
力引导布局最早由Peter Eades在1984年的“启发式画图算法”一文中提出,目的是减少布局中边的交叉,尽量保持边的长度一致。此方法借用弹簧模型模拟布局过程:用弹簧模拟两个点之间的关系,受到弹力的作用后,过近的点会被弹开而过远的点被拉近;通过不断的迭代,整个布局达到动态平衡,趋于稳定。其后,“力引导”的概念被提出,演化成力引导布局算法FR(Fruchterman-Reingold算法)——丰富两点之间的物理模型,加入点之间的静电力,通过计算系统的总能量并使得能量最小化,从而达到布局的目的。这种改进的能量模型,可看成弹簧模型的一般化。
参考:https://blog.csdn.net/gdp12315_gu/article/details/48351589
根据权重改变2节点之间线的粗细
import networkx as nx
G = nx.Graph()
G.add_edge(s,t,weight=w)
nx.draw(G,
pos = nx.spring_layout(G,iterations=200),
node_size = nodes_size_list,
node_color = nodes_colour_list,
width = [float(w['heat_value'] * 5) for (u,v,w) in G.edges(data=True)],
edge_color = "b",
edge_cmap = plt.get_cmap("viridis"),
with_labels = True,
font_weight = "bold")
复制代码