修复NetworkX弹簧图中的节点子集的位置 [英] Fix position of subset of nodes in NetworkX spring graph
问题描述
我在Python中使用Networkx,试图使不同的电影评论家偏向某些制作公司.为了在图表中显示,我的想法是将每个生产公司节点的位置固定在一个圆圈中的单个位置,然后使用spring_layout算法来定位其余的电影评论节点,以便一个人可以轻松地了解如何将某些批评家吸引到某些生产公司.
Using Networkx in Python, I'm trying to visualise how different movie critics are biased towards certain production companies. To show this in a graph, my idea is to fix the position of each production-company-node to an individual location in a circle, and then use the spring_layout algorithm to position the remaining movie-critic-nodes, such that one can easily see how some critics are drawn more towards certain production companies.
我的问题是我似乎无法确定production-company-nodes的初始位置.当然,我可以固定他们的位置,但是那只是随机的,而且我不想要-我希望他们围成一圈.我可以计算所有节点的位置,然后再设置生产公司节点的位置,但这超出了使用spring_layout算法的目的,最终得到了类似这样的古怪:
My problem is that I can't seem to fix the initial position of the production-company-nodes. Surely, I can fix their position but then it is just random, and I don't want that - I want them in a circle. I can calculate the position of all nodes and afterwards set the position of the production-company-nodes, but this beats the purpose of using a spring_layout algorithm and I end up with something wacky like:
关于如何正确执行操作的任何想法?
Any ideas on how to do this right?
当前,我的代码执行此操作:
Currently my code does this:
def get_coordinates_in_circle(n):
return_list = []
for i in range(n):
theta = float(i)/n*2*3.141592654
x = np.cos(theta)
y = np.sin(theta)
return_list.append((x,y))
return return_list
G_pc = nx.Graph()
G_pc.add_edges_from(edges_2212)
fixed_nodes = []
for n in G_pc.nodes():
if n in production_companies:
fixed_nodes.append(n)
pos = nx.spring_layout(G_pc,fixed=fixed_nodes)
circular_positions = get_coordinates_in_circle(len(dps_2211))
i = 0
for p in pos.keys():
if p in production_companies:
pos[p] = circular_positions[i]
i += 1
colors = get_node_colors(G_pc, "gender")
nx.draw_networkx_nodes(G_pc, pos, cmap=plt.get_cmap('jet'), node_color=colors, node_size=50, alpha=0.5)
nx.draw_networkx_edges(G_pc,pos, alpha=0.01)
plt.show()
推荐答案
创建图形并设置一些位置:
To create a graph and set a few positions:
import networkx as nx
G=nx.Graph()
G.add_edges_from([(1,2),(2,3),(3,1),(1,4)]) #define G
fixed_positions = {1:(0,0),2:(-1,2)}#dict with two of the positions set
fixed_nodes = fixed_positions.keys()
pos = nx.spring_layout(G,pos=fixed_positions, fixed = fixed_nodes)
nx.draw_networkx(G,pos)
您的问题似乎是在设置固定节点的位置之前先计算所有节点的位置.
Your problem appears to be that you calculate the positions of all the nodes before you set the positions of the fixed nodes.
为固定节点设置pos[p]
之后将pos = nx.spring_layout(G_pc,fixed=fixed_nodes)
移至,然后将其更改为pos = nx.spring_layout(G_pc,pos=pos,fixed=fixed_nodes)
Move pos = nx.spring_layout(G_pc,fixed=fixed_nodes)
to after you set pos[p]
for the fixed nodes, and change it to pos = nx.spring_layout(G_pc,pos=pos,fixed=fixed_nodes)
dict
pos
存储每个节点的坐标.您应该快速浏览文档一个>.尤其是
The dict
pos
stores the coordinates of each node. You should have a quick look at the documentation. In particular,
pos :字典或无"可选(默认为无"). 节点的初始位置作为字典,节点作为键,值作为列表或元组.如果为None,则nuse随机的初始位置.
pos : dict or None optional (default=None). Initial positions for nodes as a dictionary with node as keys and values as a list or tuple. If None, then nuse random initial positions.
已修复:列表或无"可选(默认为无"). 固定在初始位置的节点. 列表或无可选(默认=无)
fixed : list or None optional (default=None). Nodes to keep fixed at initial position. list or None optional (default=None)
您要告诉它将那些节点固定在其初始位置,但是您没有告诉他们该初始位置应该是什么.因此,我相信它会对该初始位置进行随机猜测,并保持固定不变.但是,当我对此进行测试时,似乎遇到了错误.看来,如果我告诉(我的版本)networkx将节点[1,2]
中的节点固定为固定,但是我不告诉他们它们的位置是什么,则会出现错误(此答案的底部).所以我很惊讶您的代码正在运行.
You're telling it to keep those nodes fixed at their initial position, but you haven't told them what that initial position should be. So I would believe it takes a random guess for that initial position, and holds it fixed. However, when I test this, it looks like I run into an error. It appears that if I tell (my version of) networkx to hold nodes in [1,2]
as fixed, but I don't tell it what their positions are, I get an error (at bottom of this answer). So I'm surprised your code is running.
对于使用列表推导的代码的其他一些改进:
For some other improvements to the code using list comprehensions:
def get_coordinates_in_circle(n):
thetas = [2*np.pi*(float(i)/n) for i in range(n)]
returnlist = [(np.cos(theta),np.sin(theta)) for theta in thetas]
return return_list
G_pc = nx.Graph()
G_pc.add_edges_from(edges_2212)
circular_positions = get_coordinates_in_circle(len(dps_2211))
#it's not clear to me why you don't define circular_positions after
#fixed_nodes with len(fixed_nodes) so that they are guaranteed to
#be evenly spaced.
fixed_nodes = [n for n in G_pc.nodes() if n in production_companies]
pos = {}
for i,p in enumerate(fixed_nodes):
pos[p] = circular_positions[i]
colors = get_node_colors(G_pc, "gender")
pos = nx.spring_layout(G_pc,pos=pos, fixed=fixed_nodes)
nx.draw_networkx_nodes(G_pc, pos, cmap=plt.get_cmap('jet'), node_color=colors, node_size=50, alpha=0.5)
nx.draw_networkx_edges(G_pc,pos, alpha=0.01)
plt.show()
这是我看到的错误:
Here's the error I see:
import networkx as nx
G=nx.Graph()
G.add_edge(1,2)
pos = nx.spring_layout(G, fixed=[1,2])
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-4-e9586af20cc2> in <module>()
----> 1 pos = nx.spring_layout(G, fixed=[1,2])
.../networkx/drawing/layout.pyc in fruchterman_reingold_layout(G, dim, k, pos, fixed, iterations, weight, scale)
253 # We must adjust k by domain size for layouts that are not near 1x1
254 nnodes,_ = A.shape
--> 255 k=dom_size/np.sqrt(nnodes)
256 pos=_fruchterman_reingold(A,dim,k,pos_arr,fixed,iterations)
257 if fixed is None:
UnboundLocalError: local variable 'dom_size' referenced before assignment
这篇关于修复NetworkX弹簧图中的节点子集的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!