问题描述
我对 Python 非常陌生,并开始学习 networkx 来绘制道路网络图.我必须指定节点位置.边缘颜色应取决于边缘的权重.我尝试使用熊猫数据框来生成边缘.未指定位置时,边缘颜色工作正常.附上代码.
将pandas导入为pd将 networkx 导入为 nx导入 matplotlib.pyplot 作为 plt# 必要的文件路径l1 = 'file1.xlsx'l2 = "file2.xlsx"n1 = pd.read_excel(l1)n1.head(10)
n2 = pd.read_excel(l2)n2.head(2)
# 构建图G = nx.from_pandas_edgelist(n1, '起始节点', '结束节点', create_using=nx.Graph())# 定义位置位置 = {}对于 G.nodes() 中的 n:i = n2[n2["节点"] == n].index[0]pos[n] = (n2.loc[i, "x"], n2.loc[i, "y"])# 绘制图形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)# 保存图形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)
但是,颜色不符合容量值.我将分享一个例子.这是它不必要地改变颜色的情况
另一个例子 - 这里的颜色应该改变,但它没有
I am pretty new to Python and started learning networkx to plot a graph for a road network. I have to specify, the node positions. The edge color should be dependent on the weights of the edges. I tried using pandas dataframe to generate edges. The edge colors work fine when position is not specified. The code is attached.
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)
However, the colors do not follow the capacity values. I will share an example.Here is the case where it unnecessarily changes color
Another example - Here the colors should change, but it does not
The excel files are here for reference
Also, if you can suggest how to add a color-bar to this, it would be great.
import networkx as nx
# dummy data, Graph and positions
df = pd.DataFrame({
'node1': np.random.choice([*'ABCDEFGHIJKL'], 10, replace=True),
'node2': np.random.choice([*'ABCDEFGHIJKL'], 10, replace=True),
'Capacity': np.random.rand(10)
})
G = nx.from_pandas_edgelist(df, source='node1', target='node2', edge_attr='Capacity')
pos = nx.spring_layout(G)
# extract the edge weight
edge_colors = [a['Capacity'] for u,v,a in G.edges(data=True)]
# draw nodes and edges separately. allows using colormap for edges.
nx.draw_networkx_nodes(G, pos=pos)
nx.draw_networkx_edges(G, pos=pos, edge_color=edge_colors, edge_cmap=plt.cm.viridis, edge_vmin=0, edge_vmax=np.max(edge_colors), width=5)
nx.draw_networkx_labels(G, pos=pos);
这篇关于Python Networkx with_pandas_edgelist:边缘没有采用正确的颜色,同时指定节点位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!