【发布时间】:2021-09-27 03:18:17
【问题描述】:
我用 networkx 完成了一个图表,用于可视化 CSV 文件中的关系,该文件大约 500 行长。为了提高可视化效果,我尝试为特定边缘着色。
-
理想情况下只有着色:(any to A), (any to B), (A to any), (B to any)。
-
大约有 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'), ('A', '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()
【问题讨论】:
标签: python csv graph nodes networkx