首页 > 解决方案 > 如何根据(核心)值对节点进行分组,为它们着色并以同心布局绘制

问题描述

在networkx中将节点分组这与我的问题类似,但我无法根据值对节点进行分组。

我想将值 > 0.67 的节点分组为第一个圆圈,将值 < 0.67 和 >0 的节点分组为第二个圆圈,将值为 0 的节点分组为最后一个圆圈。

df = pd.read_csv("D:2004-2007m.csv")
G = nx.from_pandas_edgelist(df, source='Source',target='Target')

graph = nx.from_pandas_edgelist(df, source='Source',target='Target')

rossa = cpnet.Rossa()  # Load the instance of Rossa's algorithm
rossa.detect(G)  # Detect core-periphery structures
c = rossa.get_pair_id()  # Get the group membership of nodes
x = rossa.get_coreness()  # Get the coreness of nodes

for w  in sorted(x, key=x.get, reverse=True ):
print(w, x[w])


USA 1.0
England 0.9277389277389277
Netherlands 0.8708487084870848
Canada 0.8206451612903226
spain 0.772972972972973
Germany 0.723404255319149
PeoplesRChina 0.6755952380952381
Australia 0.628125
Turkey 0.5789473684210527
Greece 0.532871972318339
HongKong,PeoplesRChina 0.48816029143898
France 0.44529750479846447
Sweden 0.4080808080808081
Japan 0.37473684210526315
Taiwan 0.34146341463414637
Norway 0.3076923076923077
Austria 0.2800982800982801
Israel 0.2532299741602067
India 0.22527472527472528
Mexico 0.19825072886297376
Wales 0.17901234567901234
NewZealand 0.1592356687898089
Guatemala 0.14046822742474915
Belgium 0.12631578947368421
CzechRepublic 0.1111111111111111
Switzerland 0.09448818897637795
Ireland 0.07627118644067797
Nigeria 0.06167400881057269
Hungary 0.04716981132075472
Argentina 0.0380952380952381
UArabEmirates 0.02912621359223301
Indonesia 0.02
Slovenia 0.010309278350515464
Korea 0.0
Italy 0.0
Brazil 0.0
Russia 0.0
Portugal 0.0
Oman 0.0
Thailand 0.0
Cyprus 0.0
Poland 0.0
Scotland 0.0
SouthAfrica 0.0
Ukraine 0.0
Bulgaria 0.0
Kuwait 0.0
Lebanon 0.0
Uruguay 0.0
Mauritius 0.0
Bolivia 0.0
Malaysia 0.0
Iceland 0.0
Serbia 0.0
SaudiArabia 0.0
Croatia 0.0
SriLanka 0.0
Morocco 0.0
Lithuania 0.0
Luxembourg 0.0
Slovakia 0.0
Peru 0.0
Jordan 0.0
Estonia 0.0
CostaRica 0.0
Pakistan 0.0
Tunisia 0.0
SerbiaMonteneg 0.0
Latvia 0.0

nodes_by_color = #assigning colors after grouping nodes based on values 


# code to plot in concentric layout
import numpy as np
pos = nx.circular_layout(graph)
radii = [7,15,30,45,60]  # for concentric circles

for ea in pos.keys():
new_r = 1
if ea in nodes_by_color['green']:
    new_r = radii[0]
elif ea in nodes_by_color['royalblue']:
    new_r = radii[1]
elif ea in nodes_by_color['red']:
    new_r = radii[2]
elif ea in nodes_by_color['orange']:
    new_r = radii[3]
elif ea in nodes_by_color['cyan']:
    new_r = radii[4]
else:
    pass
pos[ea] *= new_r 

标签: python-3.xmatplotlibdata-sciencenetworkx

解决方案


推荐阅读