DiGraph

目的:绘制加权有向连接图

1. 用连接边的颜色表示两个节点之间的强度

piVrNtK.png

2. 用边的权重来表示节点连接的强度

piVrtk6.png

参考:

https://networkx.org/documentation/stable/auto_examples/drawing/plot_directed.html
https://networkx.org/documentation/stable/reference/generated/networkx.drawing.nx_pylab.draw_networkx.html

0. 加载python库

1
2
3
4
import matplotlib.pyplot as plt
import networkx as nx
import matplotlib as mpl
import numpy as np

1. 用连接边的颜色表示两个节点之间的强度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'''0. 自己的权重值矩阵'''
value_new=[[0.,0.,0.,0.,0.,0.,0.,0.013],
[0.,0.,0.,0.,0.,0.,0.,0.013],
[0.,0.,0.,0.,0.,0.,0.,0.013],
[0.,0.,0.,0.,0.,0.,0.,0.],
[0.,0.,0.,0.,0.,0.,0.,0.],
[0.,0.,0.,0.,0.,0.,0.005,0.],
[0.,0.,0.,0.,0.,0.007,0.,0.008],
[0.011,0.013,0.,0.012,0.,0.01,0.009,0.016]]

''' 1. 初始化图'''
G = nx.MultiDiGraph()
pos = nx.spring_layout(G)

# 设置节点名称
row = np.array(['FL','FM','FR','TL','TR','C','P','O'])
G.add_nodes_from(['FL','FM','FR','TL','TR','C','P','O'])

''' 2. 将节点的连接边权重加入构建的图'''
for j in range(0,8):
for i in range(0,8):
if value_new[i][j] != 0:
G.add_weighted_edges_from([(row[j],row[i],value_new[i][j])])#边的起点,终点,权重
else:
pass

''' 3. 节点的设置 '''
# 节点大小
node_sizes = [1000,1000,1000,1000,1000,1000,1000,1000]
# 节点颜色
node_color = ['g','g','g','g','g','g','g','g']
# 设置节点位置
pos={'FL':(38,75),'FM':(40,80),'FR':(42,75),
'TL':(37,64),'C':(40,65), 'TR':(43,64),
'P':(38,53),'O':(42,53)}

''' 4. 边的颜色: 用连接边的颜色表示两个节点之间的强度/ 可以用边的权重来表示节点连接的强度 '''
edge_colors=[float(v['weight']) for (r,c,v) in G.edges(data=True)]
cmap = plt.cm.jet # jet、viridis、plasma、inferno、magma、cividis...

''' 5. 绘图 '''
# 绘制节点
nx.draw_networkx_nodes(G,pos,
node_size=node_sizes,
#node_color=node_color,
alpha=0.4
)
# 节点字体大小
nx.draw_networkx_labels(G,pos,font_size=15)

# 绘制连接边
edges = nx.draw_networkx_edges(G,pos,
edge_color = edge_colors,
#alpha=0.5, # 透明度
edge_cmap = cmap,
connectionstyle='arc3, rad = 0.25', # arc控制双向,rad调线条弧度
width=5,
)

pc = mpl.collections.PatchCollection(edges, cmap=cmap)
# 显示colorbar
pc.set_array(edge_colors)

ax = plt.gca()
ax.set_axis_off()
plt.colorbar(pc, ax=ax)
# 保存图片
plt.title('Weighted directed connected graph (colour of edges)')
plt.savefig("CE_Graph.png",dpi=600, bbox_inches='tight')
plt.show()

piVrNtK.png

2. 用边的权重来表示节点连接的强度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'''0. 自己的权重值矩阵'''
value_new=[[0.,0.,0.,0.,0.,0.,0.,0.013],
[0.,0.,0.,0.,0.,0.,0.,0.013],
[0.,0.,0.,0.,0.,0.,0.,0.013],
[0.,0.,0.,0.,0.,0.,0.,0.],
[0.,0.,0.,0.,0.,0.,0.,0.],
[0.,0.,0.,0.,0.,0.,0.005,0.],
[0.,0.,0.,0.,0.,0.007,0.,0.008],
[0.011,0.013,0.,0.012,0.,0.01,0.009,0.016]]

''' 1. 初始化图'''
G = nx.MultiDiGraph()
pos = nx.spring_layout(G)

row = np.array(['FL','FM','FR','TL','TR','C','P','O'])
G.add_nodes_from(['FL','FM','FR','TL','TR','C','P','O'])

''' 2. 将节点的连接边权重加入构建的图'''
for j in range(0,8):
for i in range(0,8):
if value_new[i][j] != 0:
G.add_weighted_edges_from([(row[j],row[i],value_new[i][j]*300)]) # 边的起点,终点,权重 ,因为我的权重实在太小了,所以整体扩大300
else:
pass

''' 3. 节点的设置 '''
# 节点大小
node_sizes = [1000,1000,1000,1000,1000,1000,1000,1000]
# 节点颜色
node_color = ['g','g','g','g','g','g','g','g']
# 设置节点位置
pos={'FL':(38,75),'FM':(40,80),'FR':(42,75),
'TL':(37,64),'C':(40,65), 'TR':(43,64),
'P':(38,53),'O':(42,53)}

''' 4. 边的颜色: 用连接边的颜色表示两个节点之间的强度/ 可以用边的权重来表示节点连接的强度 '''
weight=[float(v['weight']) for (r,c,v) in G.edges(data=True)]

''' 5. 绘图 '''
# 绘制节点
nx.draw_networkx_nodes(G,pos,
node_size=node_sizes,
#node_color=node_color,
alpha=0.4
)
# 节点字体大小
nx.draw_networkx_labels(G,pos,font_size=15)

# 绘制连接边
edges = nx.draw_networkx_edges(G,pos,
edge_color = 'lightseagreen',
#alpha=0.5, # 透明度
edge_cmap = cmap,
connectionstyle='arc3, rad = 0.25', # arc控制双向,rad调线条弧度
width=weight
)
# 保存图片
plt.savefig("SE_Graph.png",dpi=600, bbox_inches='tight')
plt.show()

piVrtk6.png