NetworkX不根据数据框中的Wegith提供正确的颜色
我正在尝试在毒品贩运组织之间创建冲突和联盟的图表。我希望将冲突描绘成红色边缘,并将联盟绘制为蓝色边缘。我给了每个关系的权重,以便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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论