How to use NetworkX's rescale_layout?

霸气de小男生 提交于 2021-02-11 13:11:57

问题


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.


回答1:


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()



来源:https://stackoverflow.com/questions/63496724/how-to-use-networkxs-rescale-layout

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