Python Netgraph Matplotlib刷新图

迈克尔·伯尼科

我正在研究网络的可视化,其中包括带有边缘标签的可移动节点,这些边缘标签通过流数据进行更新。目前,在进行绘图时,我正在使用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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章