Networkx使用指南

绘制网络图基本流程:

  1. 导入networkx,matplotlib包
  2. 建立网络
  3. 绘制网络 nx.draw()
  4. 建立布局 pos = nx.spring_layout美化作用
最基本画图程序

import import networkx as nx             #导入networkx包
import matplotlib.pyplot as plt 
G = nx.random_graphs.barabasi_albert_graph(100,1)   #生成一个BA无标度网络G
nx.draw(G)                               #绘制网络G
plt.savefig("ba.png")           #输出方式1: 将图像存为一个png格式的图片文件
plt.show()                            #输出方式2: 在窗口中显示这幅图像 

networkx 提供画图的函数有:

  1. draw(G,[pos,ax,hold])
  2. draw_networkx(G,[pos,with_labels])
  3. draw_networkx_nodes(G,pos,[nodelist])绘制网络G的节点图
  4. draw_networkx_edges(G,pos[edgelist])绘制网络G的边图
  5. draw_networkx_edge_labels(G, pos[, ...]) 绘制网络G的边图,边有label
    ---有layout 布局画图函数的分界线---
  6. draw_circular(G, **kwargs) Draw the graph G with a circular layout.
  7. draw_random(G, **kwargs) Draw the graph G with a random layout.
  8. draw_spectral(G, **kwargs)Draw the graph G with a spectral layout.
  9. draw_spring(G, **kwargs)Draw the graph G with a spring layout.
  10. draw_shell(G, **kwargs) Draw networkx graph with shell layout.
  11. draw_graphviz(G[, prog])Draw networkx graph with graphviz layout.

networkx 画图参数:

  • 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: 节点标签字体颜色(默认为黑色)
  • pos(dictionary, optional): A dictionary with nodes as keys and positions as values.If not specified a spring layout positioning will be computed.

布局指定节点排列形式

  • 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

Random Geometric Graph

画随机几何图,找出中心节点并按路径长度染色

import networkx as nx
import matplotlib.pyplot as plt

G=nx.random_geometric_graph(200,0.125)
# position is stored as node attribute data for random_geometric_graph
#`random_geometric_graph`(*n*, *radius*, *dim=2*, *pos=None*)[]
#如果两点之间的距离小于redius则相连,建立边
pos=nx.get_node_attributes(G,'pos')

# find node near center (0.5,0.5)
dmin=1
ncenter=0
for n in pos:
    x,y=pos[n]
    d=(x-0.5)**2+(y-0.5)**2
    if d

你可能感兴趣的:(Networkx使用指南)