python networkx 求图中的环_python – 修复NetworkX弹簧图中节点子集的位置

在Python中使用Networkx,我试图想象出不同的电影评论家如何偏向某些制作公司.为了在图表中显示这一点,我的想法是将每个生产公司节点的位置固定到一个圆圈中的单个位置,然后使用spring_layout算法定位剩余的电影评论节点,这样一个人可以很容易看看一些评论家如何更多地吸引某些制片公司.

我的问题是我似乎无法修复生产公司节点的初始位置.当然,我可以修复他们的位置但是它只是随机的,我不想要那个 – 我想要它们在一个圆圈里.我可以计算所有节点的位置,然后设置生产公司节点的位置,但这胜过使用spring_layout算法的目的,我最终得到了一些古怪的东西:

关于如何做到这一点的任何想法?

目前我的代码执行此操作:

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

解决方法:

要创建图表并设置几个位置:

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)

您的问题似乎是在设置固定节点的位置之前计算所有节点的位置.

将pos = nx.spring_layout(G_pc,fixed = fixed_nodes)移动到为固定节点设置pos [p]后,将其更改为pos = nx.spring_layout(G_pc,pos = pos,fixed = fixed_nodes)

dict pos存储每个节点的坐标.你应该快速浏览一下the documentation.特别是,

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]中的节点保持为固定,但我不告诉它它们的位置是什么,我得到一个错误(在这个答案的底部).所以我很惊讶您的代码正在运行.

对于使用列表推导的代码的其他一些改进:

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

这是我看到的错误:

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)

in ()

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

标签:python,networkx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值