import pandas as pd import networkx as nx import matplotlib.pyplot as plt df = pd.read_excel(r"C:\Users\li'yi'jie\Desktop\运筹学网络规划数据.xlsx") edges = [] for i in range(len(df)): edge = { "id": df.loc[i, "边的编号"], "tail": df.loc[i, "边的尾节点"], "head": df.loc[i, "边的头节点"], "length": df.loc[i, "长度"], "capacity": df.loc[i, "容量"] } edges.append(edge) G = nx.DiGraph() for edge in edges: G.add_edge(edge["tail"], edge["head"], weight=edge["length"]) all_pairs = dict(nx.all_pairs_dijkstra_path_length(G)) pos = nx.spring_layout(G) nx.draw(G, pos, with_labels=True) labels = nx.get_edge_attributes(G, "长度") nx.draw_networkx_edge_labels(G, pos, edge_labels=labels) plt.show()如何修改使生成的网络图的边旁标注权重?
时间: 2024-01-02 10:03:03 浏览: 59
数据图标分析-用networkx模块解读人物关系-Python实例源码.zip
可以在代码最后添加以下语句:
```
labels = nx.get_edge_attributes(G, "weight")
nx.draw_networkx_edge_labels(G, pos, edge_labels=labels)
plt.show()
```
其中 `labels` 变量用于获取图 G 中所有边的权重信息,`nx.draw_networkx_edge_labels()` 函数用于在图形中绘制边旁的权重标签。最后使用 `plt.show()` 函数显示图形即可。
阅读全文