简体   繁体   中英

How to make color of nodes in the network graph change sequentially from the center to the edge in Networkx

I want to draw a graph whose nodes' color in the network graph change sequentially from the center to the edge as followed:

图片样本

When I was visualizing the data, I didn't know how to deal with outliers right, so I just shuffle the data and use the most of it. Here is my code and picture:

data = pd.read_csv('soc-sign-bitcoinalpha.csv', names=['SOURCE', 'TARGET', 'RATING', 'TIME'])
new_data = data.iloc[:, :2].copy()
new_data = new_data.reindex(np.random.permutation(new_data.index))
new_data = new_data.iloc[: 18000]
G = nx.from_pandas_edgelist(new_data1, "SOURCE", "TARGET")
nx.draw(G, node_size=5, edge_vmin=1, edge_vmax=2)

我的照片

And the data is from Bitcoin Alpha trust weighted signed network in Stanford Large Network Dataset Collection .

So, if not mind, could anyone teach me how to implement it?

I really did a lot of searches before, but I just couldn't figure it out.

Thanks sincerely!

You can color by distance from center by first calculating the distance each node is from center. nx.draw uses sprint_layout to position the nodes, so you can explicitly call that to get the position of the nodes. The positions are centered on (0, 0) , so you can simply calculate the distance from center using r = np.sqrt(x**2 + y**2) :

pos = nx.spring_layout(G)
node_colors = [np.sqrt((xy**2).sum()) for xy in pos.values()]
nx.draw(G, pos, node_color=node_colors, node_size=5, edge_vmin=1, edge_vmax=2)

FYI, the example you give is likely colored by the nodes' betweenness centrality . To color by this metric, the idea is the same:

bc = nx.betweenness_centrality(G)
node_colors = list(bc.values())
nx.draw(G, node_color=node_colors)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM