绘制包含多个组件的图形时节点大小不正确 [英] Node sizes not correct when drawing a graph with many components
问题描述
我有一个包含许多组件的图表,我想对其进行可视化.作为一个特殊的特征,巨型组件中节点的节点点将随着它们的特征向量中心性而缩放.所有其他节点的大小相同.
I've got a graph with many components which I would like to visualize. As a special feature, the node dots of the nodes in the giant component shall scale with their eigenvector centrality. All the other nodes have same size.
我使用以下脚本:
import networkx as nx
import pylab as py
import matplotlib.pyplot as plt
H = nx.read_gexf(input_file)
print nx.info(H)
#Name:
#Type: Graph
#Number of nodes: 719
#Number of edges: 620
#Average degree: 1.7246
# Set draw() parameters
node_sizes = dict.fromkeys(H.nodes(), 0.005)
# Make node size of giant component nodes proportional to their eigenvector
eigenvector = nx.eigenvector_centrality_numpy(G)
for node, value in eigenvector.iteritems():
node_sizes[node] = round(value, 4)
node_sizes = [v*2000 for v in node_sizes.values()] # rescale
node_positions = nx.pygraphviz_layout(H, prog="neato")
# Draw graph with different color for each connected subgraph
plt.figure(3, figsize=(90,90))
nx.draw(H, font_size=10, pos=node_positions, node_size=node_sizes, vmin=0.0, vmax=1.0, with_labels=True)
plt.show()
一切都非常正确,因为我检查了不同的输出.但是,我收到一个输出,其中来自巨型组件以外的组件的某些节点是缩放的.此外,巨型组件中的节点没有正确缩放.
Everything is quite correct, as I checked in distinct outputs. However, I receive an output where some nodes from components other than the giant component are scale. Moreover, the nodes in the giant component are not correctly scaled.
此快照显示了巨型组件和带有缩放节点的非组件:
This snapshot shows the giant component and an off-component with a scaled node:
但是,如果我只使用字典 eigenvector
作为节点大小打印巨型组件 G
,我会得到以下 - 正确 - 输出 (:
However, if I only print the giant component G
using the dictionary eigenvector
for the node size, I get the following - correct - output (:
我也做了一些故障排除.例如,字典/列表 node_sizes
都是正确的.有趣的是,使用随机图 H = nx.fast_gnp_random_graph(300, 0.005, seed=5)
会返回正确的结果.因此,我完全不知道我的 H
出了什么问题.
I did some troubleshooting, too. For example, the dictionary/list node_sizes
is all correct. Interestingly, using a random graph H = nx.fast_gnp_random_graph(300, 0.005, seed=5)
returns correct results. Therefore I have absolutely no idea what's wrong with my H
.
推荐答案
您会注意到 node_sizes
是一个列表.您尚未向 draw 命令发送节点列表.它将从网络中的节点动态生成它们.当这两个列表的顺序不同时,就会出现问题.我认为拥有多个组件不是问题,而是您的网络越大,它们的排列顺序就越有可能不同.
You'll notice that node_sizes
is a list. You haven't sent the draw command a list of nodes. It's going to generate them on the fly from the nodes in the network. The problem occurs when these two lists end up being in different orders. I don't think it's an issue with having multiple components, but rather the larger your network is the more likely it is that they aren't put into the same order.
所以而不是
node_sizes = [v*2000 for v in node_sizes.values()]
使用
nodelist, node_sizes = zip(*node_sizes.items())
这里 nodelist 将获取 node_sizes.items 的每个条目中第一个数字的列表,node_sizes 将获取每个条目中第二个数字的列表.
here nodelist will get the list of the first numbers in each entry of node_sizes.items and node_sizes will get the list of the second numbers in each entry.
然后在绘图命令中给它节点列表
Then in the plotting command give it the nodelist
nx.draw(H, font_size=10, pos=node_positions, node_size=node_sizes, vmin=0.0, vmax=1.0, with_labels=True, nodelist=nodelist)
这篇关于绘制包含多个组件的图形时节点大小不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!