有向图#

使用颜色映射和不同节点大小绘制带有方向边的图。

边具有不同的颜色和不透明度(透明度)。使用matplotlib绘制。

plot directed
import matplotlib as mpl
import matplotlib.pyplot as plt
import networkx as nx

seed = 13648  # 为可重复性设置随机数生成器的种子
G = nx.random_k_out_graph(10, 3, 0.5, seed=seed)
pos = nx.spring_layout(G, seed=seed)

node_sizes = [3 + 10 * i for i in range(len(G))]
M = G.number_of_edges()
edge_colors = range(2, M + 2)
edge_alphas = [(5 + i) / (M + 4) for i in range(M)]
cmap = plt.cm.plasma

nodes = nx.draw_networkx_nodes(G, pos, node_size=node_sizes, node_color="indigo")
edges = nx.draw_networkx_edges(
    G,
    pos,
    node_size=node_sizes,
    arrowstyle="->",
    arrowsize=10,
    edge_color=edge_colors,
    edge_cmap=cmap,
    width=2,
)
# 为每条边设置alpha值
for i in range(M):
    edges[i].set_alpha(edge_alphas[i])

pc = mpl.collections.PatchCollection(edges, cmap=cmap)
pc.set_array(edge_colors)

ax = plt.gca()
ax.set_axis_off()
plt.colorbar(pc, ax=ax)
plt.show()

Total running time of the script: (0 minutes 0.074 seconds)

Gallery generated by Sphinx-Gallery