如何解决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)],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,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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。