注意
跳到末尾下载完整的示例代码。
三元组#
根据 Snijders, T. (2012) 的论文“传递性与三元组”(牛津大学),共有 16 种可能的三元组类型。该图展示了在有向网络中可以识别出的 16 种三元组类型。在分析社交网络时,三元组关系特别有用。前三位数字表示互惠、非对称和空二元组(双向、单向和无边)的数量,字母表示方向(Orientation),分别为向上(Up,U)、向下(Down,D)、循环(Cyclical,C)或传递(Transitive,T)。

import networkx as nx
import matplotlib.pyplot as plt
fig, axes = plt.subplots(4, 4, figsize=(10, 10))
triads = {
"003": [],
"012": [(1, 2)],
"102": [(1, 2), (2, 1)],
"021D": [(3, 1), (3, 2)],
"021U": [(1, 3), (2, 3)],
"021C": [(1, 3), (3, 2)],
"111D": [(1, 2), (2, 1), (3, 1)],
"111U": [(1, 2), (2, 1), (1, 3)],
"030T": [(1, 2), (3, 2), (1, 3)],
"030C": [(1, 3), (3, 2), (2, 1)],
"201": [(1, 2), (2, 1), (3, 1), (1, 3)],
"120D": [(1, 2), (2, 1), (3, 1), (3, 2)],
"120U": [(1, 2), (2, 1), (1, 3), (2, 3)],
"120C": [(1, 2), (2, 1), (1, 3), (3, 2)],
"210": [(1, 2), (2, 1), (1, 3), (3, 2), (2, 3)],
"300": [(1, 2), (2, 1), (2, 3), (3, 2), (1, 3), (3, 1)],
}
for (title, triad), ax in zip(triads.items(), axes.flatten()):
G = nx.DiGraph()
G.add_nodes_from([1, 2, 3])
G.add_edges_from(triad)
nx.draw_networkx(
G,
ax=ax,
with_labels=False,
node_color=["green"],
node_size=200,
arrowsize=20,
width=2,
pos=nx.planar_layout(G),
)
ax.set_xlim(val * 1.2 for val in ax.get_xlim())
ax.set_ylim(val * 1.2 for val in ax.get_ylim())
ax.text(
0,
0,
title,
fontsize=15,
fontweight="extra bold",
horizontalalignment="center",
bbox={"boxstyle": "square,pad=0.3", "fc": "none"},
)
fig.tight_layout()
plt.show()
脚本总运行时间: (0 分钟 1.070 秒)