[英]Networkx: how to add edge labels from csv file in a graph
如何將 csv/excel 文件中的 Edge label 添加到 networkx 有向圖
我想從 csv 文件中的 Edge_label 列向我的 networkx 圖中添加標簽
import pandas as pd
import matplotlib.pyplot as plt
#%matplotlib inline
import networkx as nx
df = pd.read_csv('Trail_data.csv')
g = nx.from_pandas_edgelist(df,
'Source',
'Target',
create_using=nx.DiGraph() # For Directed Route arrows
)
plt.figure( figsize=(40, 40)
)
nx.draw(g,
with_labels=True,
node_size= 3000,#k=200,
node_color='#82CAFF',##00b4d9
font_size=16,
font_weight ='bold',
font_color='black',
edge_color = ('#E55451','#810541','#00FF00'),
node_shape='o',
width=4 ,
arrows=True, #Show arrow From and To
pos=nx.random_layout(g),iterations=20,
connectionstyle='arc3, rad =0.11' #To avoid overlapping edgs
)
plt.savefig('Visualization.jpeg',
dpi = (100)
)
** 我還想用 python-dash 將此有向圖轉換為交互式圖 **
根據from_pandas_edgelist
的文檔,您可以簡單地使用edge_attr
指定列列表。
在您的情況下,您將獲得所需的圖表:
g = nx.from_pandas_edgelist(df,
'Source',
'Target',
edge_attr=`Edge_label`,
create_using=nx.DiGraph(),)
對於繪圖,您目前僅繪制節點標簽。 您可以使用draw_networkx_edge_labels
添加邊緣標簽
pos = nx.random_layout(g)
nx.draw(g,
pos=pos, ...) # add other parameters
edge_labels = nx.get_edge_attributes(g, "Edge_label")
nx.draw_networkx_edge_labels(g, pos, edge_labels)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.