NetworkX不根据数据框中的Wegith提供正确的颜色

发布于 2025-01-23 10:19:39 字数 933 浏览 3 评论 0原文

我正在尝试在毒品贩运组织之间创建冲突和联盟的图表。我希望将冲突描绘成红色边缘,并将联盟绘制为蓝色边缘。我给了每个关系的权重,以便NetworkX可以给每个关系带来不同的颜色。这是我拥有的Edgelist:

但是所显示的颜色与重量不一致。我尝试了不同的事情,但似乎没有任何作用。这是我的代码:


import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df=pd.read_csv("2004N.csv")
df

    Source  Target  Weight
0   Gulf Cartel     Sinaloa Cartel  -1
1   Tijuana Cartel  Sinaloa Cartel  -1
2   Gulf Cartel     Juarez Cartel   1
3   Tijuana Cartel  Gulf Cartel     1
4   Tijuana Cartel  Juarez Cartel   1
5   Juarez Cartel   Sinaloa Cartel  -1

 

G=nx.from_pandas_edgelist(df, 'Source', 'Target', edge_attr="Weight")

plt.figure(1,figsize=(12,8))

nx.draw(G, with_labels=True, node_color='skyblue', node_size=1500, edge_color=df['Weight'], width=5.0)

这是我得到的图像:

边缘的颜色与重量不一致

任何帮助将不胜感激。

I am trying to create graphs of conflicts and alliances between drug trafficking organizations. I want conflicts to be graphed as red edges, and alliances to be graphed as blue edges. I gave each relationship a weight so that Networkx could give each relationship a different color. This is the edgelist I have:

But the colors shown are inconsistent with the weights. I have tried different things but nothing seems to work. This is my code:


import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df=pd.read_csv("2004N.csv")
df

    Source  Target  Weight
0   Gulf Cartel     Sinaloa Cartel  -1
1   Tijuana Cartel  Sinaloa Cartel  -1
2   Gulf Cartel     Juarez Cartel   1
3   Tijuana Cartel  Gulf Cartel     1
4   Tijuana Cartel  Juarez Cartel   1
5   Juarez Cartel   Sinaloa Cartel  -1

 

G=nx.from_pandas_edgelist(df, 'Source', 'Target', edge_attr="Weight")

plt.figure(1,figsize=(12,8))

nx.draw(G, with_labels=True, node_color='skyblue', node_size=1500, edge_color=df['Weight'], width=5.0)

This is the image I get:

Color of edges are not consistent with weights

Any help will be very much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文