【发布时间】:2020-08-28 16:35:09
【问题描述】:
我正在尝试改进我的绘图功能。我想使用来自脑电图板的plotGraph 函数实时 绘制数据,从 LSL @ 250Hz 中提取样本。以前,我有一个使用常规self.ax.plot(x,y) 的功能版本,每次需要刷新绘图时都会使用self.ax.clear() 清除数据。尽管如此,一些分析表明,与其他部分相比,我的代码花费了太多时间来绘制。
我得到的一个建议是使用 set_data 而不是 plot and clear。我有多行数据要同时绘制,所以我尝试关注Matplotlib multiple animate multiple lines,您可以在下面看到(改编代码)。另外,有人告诉我使用self.figure.canvas.draw_idle(),我试过了,但我不确定我是否正确。
不幸的是,它不起作用,图表没有更新,我似乎找不到原因。我知道我刚才提到的来源使用animation.FuncAnimation,但我不确定这会是问题所在。是吗?
关于为什么我的画布图表中没有显示我的线条有什么想法吗?
import tkinter as tk
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
class AppWindow:
def plotGraph(self, x, y):
for lnum,line in enumerate(self.lines):
line.set_data(x[:], y[:, lnum])
self.figure.canvas.draw_idle()
plt.ylabel('Magnitude', fontsize = 9, color = tx_color)
plt.xlabel('Freq', fontsize = 9, color = tx_color)
self.figure.canvas.draw()
def __init__(self):
self.root = tk.Tk() #start of application
self.canvas = tk.Canvas(self.root, height = 420, width = 780, bg =
bg_color, highlightthickness=0)
self.canvas.pack(fill = 'both', expand = True)
self.figure = plt.figure(figsize = (5,6), dpi = 100)
self.figure.patch.set_facecolor(sc_color)
self.ax = self.figure.add_subplot(111)
self.ax.clear()
self.line, = self.ax.plot([], [], lw=1, color = tx_color)
self.line.set_data([],[])
#place graph
self.chart_type = FigureCanvasTkAgg(self.figure, self.canvas)
self.chart_type.get_tk_widget().pack()
self.lines = []
numchan = 8 #let's say I have 8 channels
for index in range(numchan):
lobj = self.ax.plot([],[], lw=2, color=tx_color)[0]
self.lines.append(lobj)
for line in self.lines:
line.set_data([],[])
def start(self):
self.root.mainloop()
【问题讨论】:
-
这可能是因为缺少
draw语句。我相信在更新之一之后添加self.figure.canvas.draw()可能会奏效! -
嘿@zwep,感谢您的评论。我尝试在更新后添加
self.figure.canvas.draw(),但它仍然不起作用。
标签: python performance matplotlib tkinter