简体   繁体   English

Networkx:具有许多节点的网络

[英]Networkx: networks with many nodes

I have a dataframe, with approximately 8,800 lines, from where I want to generate a network with networkx. 我有一个数据帧,大约有8,800行,我想用networkx生成一个网络。 The basic structure of the dataframe for network generation is demonstrated below: 网络生成数据框的基本结构如下所示:

df = pd.DataFrame({'id_emp':[13524791000109, 12053850000137, 4707821000113],
               'name_dep': ['DIONILSO MATEUS MARCON', 'JOSE AUGUSTO ROSA', 'LUCIO ANTONIO MOSQUINI'],
               'roi':[12, 15, 18]
              })

In the network I want to represent, there are two types of nodes: 'id_emp' and 'name_dep' and there are edges for each pair of these values ​​that are on the same line. 在我想要表示的网络中,有两种类型的节点:'id_emp'和'name_dep',并且在同一行上有每对这些值的边。 Therefore, the network will have 17,600 nodes. 因此,网络将拥有17,600个节点。 The edges will have weights based on the roi column. 边缘将具有基于roi柱的权重。 The code to create the network follows below: 创建网络的代码如下:

import networkx as nx
G = nx.Graph()
G.add_nodes_from(df['id_emp'], type_='id_emp')
for node in G.nodes:
    G.nodes[node]['fornecedor'] = df[df['id_emp'] == node]['fornecedor'].values[0]

G.add_nodes_from(df['name_dep'], type_='name_dep')

G.add_weighted_edges_from(df[['id_emp', 'name_dep', 'roi']].values)

colors = ['#0000FF' if G.nodes[n]['type_'] == 'id_emp' else '#FF0000' for n in G.nodes]

edge_width = [a[2]['weight'] for a in G.edges(data=True)]

plt.figure(figsize=(7.5,7.5))

nx.draw(G, pos = nx.kamada_kawai_layout(G), node_size = 100, 
node_color = colors, with_labels=False, edge_cmap=plt.cm.Blues)
nx.draw_networkx_edges(G,pos=nx.kamada_kawai_layout(G),width=edge_width)
plt.axis('off')
plt.show()

The network with the original dataframe looks like this: 具有原始数据帧的网络如下所示:

在此输入图像描述

My questions are as follows: Is it appropriate to plot networks with this amount of nodes using networkx? 我的问题如下:使用networkx绘制具有此数量节点的网络是否合适? If yes, how can I improve the network view? 如果是,我该如何改进网络视图? If not, what should I do to improve the network view? 如果没有,我该怎么做才能改善网络视图?

I think that is partly because your node size is too big which makes the whole image seems chaotic. 我认为这部分是因为你的节点大小太大,这使得整个图像看起来很混乱。 You can change it from 100 to much smaller value and see if it meets your need. 您可以将其从100更改为更小的值,并查看它是否符合您的需要。

Moreover, you can check if there is any better layout for this huge amount of nodes. 此外,您可以检查是否有更大的节点布局

try this code for layouts available: 尝试使用此代码进行布局:

[x for x in nx.__dir__() if x.endswith('_layout')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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