问题描述
id_1 | id_2 | 日期 |
---|---|---|
FD345 | MER3345 | 06/12/2020 |
我想连接 id_1 -> id_2
它们之间的边缘应该是日期,见下图
看到 id_1 它有一个直接连接到 id_2 的边
它们之间的边缘应该是日期
所以我所做的就是这样:
import networkx as nx
import pandas as pd
df = pd.read_csv('data.csv')
G = nx.from_pandas_edgelist(df,source = "id_1",target = "id_2",edge_attr='date',create_using=nx.DiGraph())
但通过这种方式,它没有按日期连接 node_1 和 node_2,它只给出了日期属性!!
或者我没有正确理解,因为如果我在打印 G.edges() 时这样做了输出
('UCU6lC','vOGN5A'),........
它连接节点,但我不确定它是否与日期连接!
谢谢你为我澄清一些事情。
解决方法
您需要使用 draw_networkx_edge_labels() 来绘制边缘标签。
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
df = pd.DataFrame({'id_1': ['FD345'],'id_2': ['MER3345'],'date': ['06/12/2020']
})
G = nx.from_pandas_edgelist(df,source="id_1",target="id_2",create_using=nx.DiGraph())
nx.draw_networkx(G)
nx.draw_networkx_edge_labels(G,nx.spring_layout(G),edge_labels=dict(zip(G.edges,df['date'].tolist())),verticalalignment='center_baseline')
plt.show()