我知道我回答这个问题有点晚,但对于你的问题,你可以查看“joystick”包。我设计它用于绘制从串口传输的数据流,但它适用于任何数据流。它还允许交互式文本记录或图像绘制(除了图形绘制)。
不需要在单独的线程中执行自己的循环,该软件包会处理它,只需提供所需的更新频率即可。此外,终端仍然可用于监视命令同时进行绘图。
请参见
http://www.github.com/ceyzeriat/joystick/ 或
https://pypi.python.org/pypi/joystick(使用pip install joystick安装)。
只需将下面代码中的np.random.random()替换为从串口读取的实际数据点即可:
import joystick as jk
import numpy as np
import time
class test(jk.Joystick):
_infinite_loop = jk.deco_infinite_loop()
def _init(self, *args, **kwargs):
"""
Function called at initialization, see the doc
"""
self._t0 = time.time()
self.xdata = np.array([self._t0])
self.ydata = np.array([0.0])
self.mygraph = self.add_frame(jk.Graph(name="test", size=(500, 500), pos=(50, 50), fmt="go-", xnpts=10000, xnptsmax=10000, xylim=(None, None, 0, 1)))
@_infinite_loop(wait_time=0.2)
def _generate_data(self):
"""
Loop starting with the simulation start, getting data and
pushing it to the graph every 0.2 seconds
"""
self.xdata = jk.core.add_datapoint(self.xdata, time.time(), xnptsmax=self.mygraph.xnptsmax)
self.ydata = jk.core.add_datapoint(self.ydata, np.random.random(), xnptsmax=self.mygraph.xnptsmax)
self.mygraph.set_xydata(t, self.ydata)
t = test()
t.start()
t.stop()