【发布时间】:2020-05-30 17:49:57
【问题描述】:
我对 Python 很陌生,并开始学习 networkx 来绘制道路网络图。我必须指定节点位置。边缘颜色应取决于边缘的权重。我尝试使用 pandas 数据框来生成边缘。未指定位置时,边缘颜色可以正常工作。附上代码。
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
# Necessary file-paths
l1 = 'file1.xlsx'
l2 = "file2.xlsx"
n1 = pd.read_excel(l1)
n1.head(10)
n2 = pd.read_excel(l2)
n2.head(2)
# Building graph
G = nx.from_pandas_edgelist(n1, 'Start Node', 'End Node', create_using=nx.Graph())
# Defining positions
pos = {}
for n in G.nodes():
i = n2[n2["Nodes"] == n].index[0]
pos[n] = (n2.loc[i, "x"], n2.loc[i, "y"])
# Plot the Graph
nx.draw(G, pos, with_labels=True, node_color='pink', node_size=30, alpha=0.8, font_size=2,
edge_color=n1['Capacity'], width=1.0, figsize = (18,18), style="solid", edge_cmap=plt.cm.jet)
# Save the Figure
plt.savefig('Network.png', dpi=1000, facecolor='w', edgecolor='w',orientation='portrait',
papertype=None, format=None,transparent=False, bbox_inches=None, pad_inches=0.1)
但是,颜色不遵循容量值。我将分享一个例子。 这是它不必要地改变颜色的情况
另一个例子 - 这里的颜色应该改变,但它没有
The excel files are here for reference
另外,如果您能建议如何为此添加颜色条,那就太好了。
【问题讨论】: