[英]Python Netgraph Matplotlib Refresh Plot
我正在對一個網絡進行可視化,該網絡包括帶有通過流數據更新的邊緣標簽的可移動節點。 目前,我在繪圖時使用 randint 更新熊貓數據框。
當前代碼可以生成節點並允許它們移動並更新邊緣標簽,但感覺“笨拙”,並且每隔一段時間繪圖會閃爍軸(我不想看到)。 我似乎無法在 netgraph 中找到一個好的鈎子來簡單地刷新圖形而不進行清除和重繪,隨着網絡的增長,這將不可避免地變得更糟。 有誰知道我怎樣才能讓它更順暢?
這是當前的代碼:
import pandas as pd
import matplotlib.pyplot as plt
#plt.ion()
import networkx as nx
import random as r
import netgraph
import numpy as np
#Graph creation:
G=nx.Graph(type="")
#edges automatically create nodes
df=pd.read_csv('diyNodeSet.csv')
G = nx.from_pandas_edgelist(df, source='sdr', target='rxr', \
create_using=nx.DiGraph)
#Create edge list from dataframe
df['xy']=list(zip(df.sdr,df.rxr))
ed=list(zip(df.br,df.pct))
el=dict(zip(df.xy,ed))
pos = nx.layout.circular_layout(G) ##initial node placement
# drag nodes around #########
plot_instance = netgraph.InteractiveGraph(G, node_positions=pos, node_color='red', edge_labels=el)
#update the edge labels with random data
import threading
interval = 3
def updatePlot(oldPlot):
nodePos=oldPlot.node_positions
new_pct=pd.Series([r.randint(1, 100),r.randint(1, 100),r.randint(1, 100),r.randint(1, 100)], name='pct', index=[0,1,2,3])
df.update(new_pct)
ed=list(zip(df.br,df.pct))
el=dict(zip(df.xy,ed))
oldPlot.fig.clear()
global plot_instance
plot_instance = netgraph.InteractiveGraph(G, node_positions=nodePos, node_color='red', edge_labels=el)
#call update each interval
def startTimer():
threading.Timer(interval, startTimer).start()
updatePlot(plot_instance)
startTimer()
這是 Netgraph ( here ) 的作者的回復,它避免了重繪繪圖並刪除了出現的刻度:
def updatePlot(plot_instance):
new_labels = ... # get your label dict
plot_instance.draw_edge_labels(plot_instance.edge_list, new_labels,
plot_instance.node_positions)
plot_instance.fig.canvas.draw_idle()
這會添加新的邊標簽並更新現有的邊標簽。 如果要刪除邊緣標簽,則必須明確刪除它們。 藝術家存儲在將邊緣映射到藝術家的字典中。
for edge in edge_labels_to_remove:
plot_instance.edge_label_artists[edge].remove() # delete artist
del plot_instance.edge_label_artists[edge] # delete reference
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.