我正在尝试将每秒更新一次的 matplotlib 图嵌入到 PyQt GUI 主窗口中。
在我的程序中,我每秒调用一个更新函数threading.Timer
通过timer
函数如下所示。我有一个问题:我的程序每秒都在变大——大约每 4 秒 1k。我最初的想法是追加函数(返回一个新数组update_figure
)不删除旧数组?这有可能是我的问题的原因吗?
def update_figure(self):
self.yAxis = np.append(self.yAxis, (getCO22()))
self.xAxis = np.append(self.xAxis, self.i)
# print(self.xAxis)
if len(self.yAxis) > 10:
self.yAxis = np.delete(self.yAxis, 0)
if len(self.xAxis) > 10:
self.xAxis = np.delete(self.xAxis, 0)
self.axes.plot(self.xAxis, self.yAxis, scaley=False)
self.axes.grid(True)
self.i = self.i + 1
self.draw()
这是我的计时器函数 - 这是通过单击 PyQt GUI 中的按钮触发的,然后调用自身,如您所见:
def timer(self):
getCH4()
getCO2()
getConnectedDevices()
self.dc.update_figure()
t = threading.Timer(1.0, self.timer)
t.start()
EDIT:我无法发布我的整个代码,因为它需要大量的 .dll 包含。所以我将尝试解释这个程序的作用。
In my GUI I want to show the my CO2 value over time. My get_co22
function just returns a float value and I'm 100% sure this works fine. With my timer, shown above, I want to keep append a value to a matplotlib graph - the Axes
object is available to me as self.axes
. I try to plot the last 10 values of the data.
EDIT 2:经过一些聊天中的讨论,我尝试拨打电话update_figure()
in a while
循环并仅使用一个线程来调用它,并且能够制作这个最小的示例http://pastebin.com/RXya6Zah。这改变了调用的代码的结构update_figure()
至以下内容:
def task(self):
while True:
ui.dc.update_figure()
time.sleep(1.0)
def timer(self):
t = Timer(1.0, self.task())
t.start()
但现在程序在 5 次迭代左右后崩溃了。