如何更新情节事件驱动的? [英] How to update a plot event driven?
问题描述
如何更新驱动的matplotlib绘图事件?
How can I update a matplotlib plot event driven?
背景: 我想编写一个工具,向我显示通过串行端口接收到的测量值. 这些值将由串行连接的设备在具有测量值时发送,而不是在主机请求时发送.
Background: I wanna program a tool that displays me measured values I receive via a serial port. These values will be send by a serial connected device when it has measured values and not when the host requests.
当前状态:
似乎没有办法手动更新绘图.
当我多次调用plot()
函数时,将添加新图.与MATLAB或Octave相似,当您使用保持"选项时.
大约10次通话后结束.然后将其冻结.
当我在更新前清除数字时,整个图都消失了.
至少当绘图被嵌入到窗口中时.
draw()
和show()
均未提供补救措施.
Current State:
It seems there is no way to update a plot manually.
When I call the plot()
-function more than one time the new plots will be added. Similar to MATLAB or Octave, when you use the "hold on"-Option.
This ends after about 10 calls. Then it is freezed.
When I clear the figures before update, the whole plot disappears.
At least, when the plot is embedded in a window.
Neither draw()
nor show()
provide a remedy.
当我使用单个绘图时,没有简单的更新方法,因为在调用show()
之后,程序流会停留在这一行,直到关闭Window.因此,更新必须在单独的线程中完成.
When I use a single plot figure, there is no easy way to update, because after the call of show()
, the program flow sticks at this line, until the Window is closed. So the update has to be done in a separate thread.
当我使用动画时,两个问题都将解决:
Both problems will be solved, when I use an animation:
import sys
from PyQt4.QtGui import QApplication, QMainWindow, QDockWidget, QVBoxLayout,QTabWidget, QWidget
from PyQt4.QtCore import Qt
from matplotlib import pyplot, animation
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
from random import randint
a = QApplication(sys.argv)
w = QMainWindow()
t = QTabWidget(w)
Tab1 = QWidget()
t.addTab(Tab1, '1st Plot')
t.resize(1280, 300)
x = [1, 2, 3]
Fig1 = pyplot.Figure();
Plot = Fig1.add_subplot(111);
Plot.plot(x)
Plot.grid();
layout = QVBoxLayout();
layout.addWidget(FigureCanvasQTAgg(Fig1));
Tab1.setLayout(layout);
def UpdateFunction(i):
x.append(randint(0, 10));
Plot.clear();
Plot.plot(x);
Plot.grid();
Anim = animation.FuncAnimation(Fig1, UpdateFunction, interval=500)
w.showMaximized()
sys.exit(a.exec_())
但是这种动画是时间触发的. 有什么解决方案可以更新触发的事件?
But such an animation is time triggered. Is there any solution to update event triggered?
该事件应该是完成的UART读入.
The event should be a finished UART read in.
非常感谢. 法比安
推荐答案
@tcaswell
@tcaswell
谢谢!
这是我的实际代码:
import sys
from PyQt4.QtGui import QApplication, QMainWindow, QVBoxLayout,QTabWidget, QWidget
from PyQt4.QtCore import SIGNAL
from matplotlib import pyplot
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
from random import randint
import time
from threading import Thread
a = QApplication(sys.argv)
w = QMainWindow()
t = QTabWidget(w)
Tab1 = QWidget()
t.addTab(Tab1, '1st Plot')
t.resize(1280, 300)
x = [];
x.append(randint(0, 10));
x.append(randint(0, 10));
x.append(randint(0, 10));
Fig1 = pyplot.Figure();
Plot = Fig1.add_subplot(111);
Line, = Plot.plot(x)
Plot.grid();
layout = QVBoxLayout();
layout.addWidget(FigureCanvasQTAgg(Fig1));
Tab1.setLayout(layout);
def triggerUpdate():
while True:
a.emit(SIGNAL('updatePlot()'));
time.sleep(2);
print('update triggered!\n')
def updateFunction():
x.append(randint(0, 10));
#Plot.clear();
Line.set_data(range(len(x)), x);
Fig1.canvas.draw_idle();
print('update done!\n')
a.connect(a, SIGNAL('updatePlot()'), updateFunction)
t = Thread(target=triggerUpdate);
t.start();
w.showMaximized()
sys.exit(a.exec_())
它似乎正在运行,但是情节的内容没有更新. 剧情没有画布. (尽管它应该有一个,否则我现在不知道它在命令时在绘制什么.)
It seems to run, but the content of the plot gets not updated. The Plot has no canvas. (Although it should have one, otherwise I don't now on what it is plotting when I command it.)
说实话,我还不了解GUI和plot foo的概念.缺少的画布由其育儿图覆盖.我真的不知道如果有图形,什么可以处理多个图,每个图都不应该有自己的画布吗?
To be honest, I did not understand already the concept of the GUI and plot foo. The missing canvas is covered by its parenting figure. I really don't got it. When there is a figure, what can handle more than one plots, should't each plot have its own canvas?
但是我认为这是问题所在.
But I think, here is the problem.
非常感谢.
这篇关于如何更新情节事件驱动的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!