如何使用NetworkX的rescale_layout? [英] How to use NetworkX's rescale_layout?

查看:81
本文介绍了如何使用NetworkX的rescale_layout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解如何使用NetworkX的

I am having a hard time understanding how to use NetworkX's rescale_layout.

The documentation says: pos (numpy array) – positions to be scaled. Each row is a position which is not the standard "position dictionary" that the rest of NetworkX uses for plotting. I have tried to use NetworkX's to_numpy_array on the "pos dict" to no success, and the output of to_numpy_array is a Graph adjacency matrix which doesn't mesh with the requirement of rescale_layout's Each position is one row of the array anyway.

The reason why I am asking is because I have a Network which is generated from some data, which is then moved around by spring_layout, but some of the positions don't generate in a 'nice' manner, so I have implemented a method to manually adjust those positions, which would be easier to do if the network was guaranteed to have positions that always lied within some predefined boundary (e.g. [-10, 10] in the plot). I then need to write the node labels just above the node.

Bonus question: I want to write the label at position (x,y+r) where r is the radius of the drawn node. I have a hacked together way of doing so by taking the square root of the input node size and dividing it by an arbitrary factor. However, I have no idea how the drawn node size is actually determined and each individual graph I make seems to have a different internal scale for each graph, while the drawn node sizes appear to remain the same.

I have trawled through a significant amount of documentation to try to understand the underlying methods, but each layer I peel back only adds more confusion.

解决方案

This is a example of your layout:

pos = nx.random_layout(G)

and it's assigned to a dictionary that looks like:

{0: array([0.81931883, 0.8001609 ], dtype=float32), 
 1: array([0.89695644, 0.6070644 ], dtype=float32), 
 2: array([0.89160234, 0.47174907], dtype=float32), 
 3: array([0.20430276, 0.8206253 ], dtype=float32), 
 4: array([0.27929142, 0.08657268], dtype=float32)}

Now, since input of nx.rescale_layout() should be numpy array, you can extract it using a command

np.array(list(pos.values()))

Note that this way might be different on other versions of Python. I'll give an illustration of what have changed after pos parameter is rescaled:

import networkx as nx
import matplotlib.pyplot as plt
import numpy as np

G = nx.Graph()
G.add_edges_from([[0,1],[1,2],[2,3],[3,4],[4,0]])
pos = nx.random_layout(G)
coords = np.array(list(pos.values()))

fig, ax = plt.subplots()
plt.subplot(211)
nx.draw_networkx(G, pos, with_labels=True)
plt.axis('on'); plt.tick_params(left=True, bottom=True, labelleft=True, labelbottom=True) #force axis to turn on

plt.subplot(212)
new_pos = nx.rescale_layout(coords)
nx.draw_networkx(G, new_pos, with_labels=True)
plt.axis('on'); plt.tick_params(left=True, bottom=True, labelleft=True, labelbottom=True) #force axis turn on
plt.show()

这篇关于如何使用NetworkX的rescale_layout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆