Node sizes not correct when drawing a graph with many components

扶醉桌前 提交于 2021-02-07 03:59:04

问题


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.

I use the following script:

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: enter image description here

However, if I only print the giant component G using the dictionary eigenvector for the node size, I get the following - correct - output (:

enter image description here

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.


回答1:


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.

So rather than

node_sizes = [v*2000 for v in node_sizes.values()]

use

nodelist, node_sizes = zip(*node_sizes.items())

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)


来源:https://stackoverflow.com/questions/29438584/node-sizes-not-correct-when-drawing-a-graph-with-many-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!