使用matpltlib每秒动态更新python中的条形图



我每隔一秒就会从rasperry pi中的传感器接收新数据,并将其附加到现有列表中。我想根据列表每秒动态更新条形图。我能做到,但这需要一秒钟多的时间。请建议如何解决此问题。在我的程序中,我保持blit=False。请帮助我如何打开条形图的闪电战,这样绘图恢复可能会更快。

class PlotAnimate(): #threading.Thread
def __init__(self):
x_vals=[0,0,0,0,0,0,0,0,0]
y_vals=[0,0,0,0,0,0,0,0,0]
data= [x_vals, y_vals]
ls_param=[0,19]
index= count()
self.fig= plt.figure(num =1,facecolor = "black")
self.ax= self.fig.add_subplot(111)
self.ax.set_facecolor("black")
plt.axis('off')
plt.tick_params(axis = "both", left = False, right = False, bottom = False, top =False)
self.bar1 = FigureCanvasTkAgg(self.fig,root.t1.frame_chart)
self.ani= FuncAnimation(self.fig, self.animate, blit= False,interval= 250)
plt.tight_layout()
self.bar1.get_tk_widget().pack(side=LEFT, fill=BOTH, expand = 1)
def animate(self,i):
#chart update
index = []
for j in range(root.t1.hist_size):#
index.append(j)
plt.tight_layout()
self.ax.cla()
plt.axis('off')
plt.tick_params(axis = "both", left = False, right = False, bottom = False, top =False)
self.ax.bar(index,root.t1.dose_list,color = root.t1.colors)#self.bar_dose,

问题是您的FuncAnimation每隔一秒就会将250 ms添加到接收新数据的时间中。所以,把它改成

frames = 100
self.ani= FuncAnimation(self.fig, self.animate, blit= True,frames=frames,
repeat=False,interval= 0)

最新更新