2016-03-04 113 views
0

我是一名網絡x(python)的初學者,我想繪製一個隨時間變化的節點位置的交互式圖形。我知道如何修復我的第一張圖的節點位置,但是如何更新節點位置?先謝謝你。networkx隨節點位置隨時間而變化

回答

1

這樣做不是微不足道的。 Networkx基於matplotlib scatter(see code here)爲節點,LineCollection爲邊緣。

要創建一個動畫圖,你應該從this matplotlib animated example得到靈感。

基本存根應該是這樣的:

# Create new Figure and an Axes which fills it. 
fig = plt.figure(figsize=(7, 7)) 
ax = fig.add_axes([0, 0, 1, 1], frameon=False) 
ax.set_xlim(0, 1), ax.set_xticks([]) 
ax.set_ylim(0, 1), ax.set_yticks([]) 

scat = nx.draw_nodes(G) 

def update(frame_number): 
    # TODO 
    # Change node position (x, y) here 
    scat.set_offsets(new_pos) 

# Construct the animation, using the update function as the animation 
# director. 
animation = FuncAnimation(fig, update, interval=10) 
plt.show() 
+0

謝謝您的答覆,但它不工作。在我看來,matplotlib動畫函數不適用於圖形。我錯了嗎? –

+0

它的工作原理。 Networkx使用matplotlib分散。 – Kikohs

+0

是的,它的工作原理!再次感謝你! –