我用 networkx 绘制了一个图表,用于从大约 500 行长的 CSV 文件中可视化关系。为了改善可视化效果,我尝试为特定边缘着色。
-
理想情况下只着色:(任何到 A)、(任何到 B)、(A 到任何)、(B 到任何)。
-
大约有 140 条边,所以我无法手动给它们上色。
-
我尝试遍历 graph.edges 并创建一个列表,如 这就是 nx.draw 期望收到的,但没有成功。
-
我已经设法用不同的颜色为所有边缘着色,但我需要有特定的颜色,仅用于某些边缘。
这就是我现在所拥有的(简化版)
用不同的颜色为每条边着色
这就是我想要的
有什么建议吗?
非常感谢:)
CSV:
from;to
A;G
B;A
C;A
D;S
V;A
V;S
V;A
M;S
M;A
...
graph.edges:
[('A','C'),('A','D'),'F'),
代码:
# Read the CSV file
df = pd.read_csv("test.csv",sep=";")
# Create the directed graph
graph = nx.from_pandas_edgelist(df,source="from",target="to",create_using=nx.DiGraph())
# Create dummy weight and assign a color
d=dict(graph.edges)
count=graph.number_of_edges()
print(count)
values = range(count)
# Plot
plt.figure(figsize=(12,12),dpi=120)
pos = nx.shell_layout(graph,scale=8)
nx.draw(graph,pos=pos,node_size=600,node_color='lightblue',edge_color=edge_colors,linewidths=0.05,font_size=6,with_labels=True ) #font_weight='bold',plt.show()