![](/img/trans.png)
[英]How to fix position of nodes when using draw_circular in networkx?
[英]How to fix the nodes position in networkx?
我最近開始在 python 中使用 Networkx。 我有一個代碼,它生成一個網絡,並根據一些過程節點功能發生變化。 例如顏色和狀態。 為了繪制圖表,我使用以下 function
def draw_graph():
colors = []
for i in range (nCount):
for j in range (i,nCount):
if ifActive(i,j,timeStep) == 1 :
colors.append('r')
else :
colors.append('g')
color_map = []
nColor= nx.get_node_attributes(graph,'color')
for nc in nColor:
color_map.append(nColor[nc])
nx.draw(graph,pos=nx.spring_layout(graph), node_color = color_map, edge_color = colors,with_labels = True )
並在for循環中的主要function中,我將繪圖稱為function,但每次position的節點都會發生變化。 現在我想知道,有沒有辦法修復所有圖紙中節點的position? 如果是,我該怎么做? 這是主要的 function
draw_graph()
for time in range(1,timeStep+1):
if graph.node[i]["status"] == 1:
settime(time,i)
plt.figure(figsize=[10, 10], dpi=50)
draw_graph()
正如@furas 在評論中所述,為了始終獲得相同的節點位置,您需要將其保留為變量,例如:
pos = nx.spring_layout(graph)
然后將您的圖表繪制為:
def draw_graph(p):
# any code here, as long as no further nodes are added.
nx.draw(graph,pos=p)
然后您最終可以將其稱為:
pos = nx.spring_layout(graph)
draw_graph(pos)
# any code here, as long as no further nodes are added.
draw_graph(pos)
# each call will give the same positions.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.