[英]Fix position of subset of nodes in NetworkX spring graph
在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
存儲每個節點的坐標。 您應該快速查看文檔 。 特別是,
pos :dict或None可選(默認=無)。 節點的初始位置作為字典,節點作為鍵,值作為列表或元組。 如果為None,則編輯隨機初始位置。
fixed :list或None可選(默認=無)。 節點保持固定在初始位置。 list或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)
<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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.