使用Python中的Networkx,我试图想象不同的电影评论家对某些制作公司的偏见。为了在图表中显示这一点,我的想法是将每个制作公司节点的位置固定到一个圆圈中的单个位置,然后使用spring_布局算法来定位剩余的影评节点,这样就可以很容易地看到一些影评人是如何更倾向于某些制作公司的。
我的问题是,我似乎无法确定生产公司节点的初始位置。当然,我可以确定他们的位置,但那只是随机的,我不想那样-我希望他们围成一个圈。我可以计算所有节点的位置,然后设置生产公司节点的位置,但这比使用spring_布局算法的目的要好,我最终得到了一些古怪的结果,比如:
你有什么想法吗?
当前我的代码执行以下操作:
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()
创建一个图形并设置几个位置:
您的问题似乎是在设置固定节点的位置之前计算所有节点的位置。
为固定节点设置
pos[p]
后将pos = nx.spring_layout(G_pc,fixed=fixed_nodes)
移动到,并将其更改为pos = nx.spring_layout(G_pc,pos=pos,fixed=fixed_nodes)
dict
pos
存储每个节点的坐标。你应该看看the documentation。尤其是你告诉它把这些节点固定在初始位置,但是你没有告诉他们初始位置应该是什么。所以我相信它需要对初始位置进行随机猜测,并保持不变。但是,当我测试这个时,似乎遇到了一个错误。如果我告诉networkx(我的版本)将节点固定在
[1,2]
中,但我不告诉它它们的位置是什么,那么我会得到一个错误(在这个答案的底部)。所以我很惊讶你的代码在运行。对于使用列表理解对代码进行的其他一些改进:
这是我看到的错误:
相关问题 更多 >
编程相关推荐