我知道我来不及回答这个问题,但是对于您的问题,您可以考虑使用“操纵杆”包装。我设计它用于从串行端口绘制数据流,但它适用于任何流。它还允许交互式文本记录或图像绘图(除了图形绘图外)。无需在单独的线程中执行自己的循环,程序包会照顾它,只需提供您希望的更新频率即可。另外,终端在绘制时仍可用于监视命令。参见http://www.github.com/ceyzeriat/joystick/或https://pypi.python.org/pypi/joystick(使用pip install游戏杆进行安装)
只需将np.random.random()替换为您从以下代码中从串行端口读取的实际数据点:
import joystick as jk
import numpy as np
import time
class test(jk.Joystick):
# initialize the infinite loop decorator
_infinite_loop = jk.deco_infinite_loop()
def _init(self, *args, **kwargs):
"""
Function called at initialization, see the doc
"""
self._t0 = time.time() # initialize time
self.xdata = np.array([self._t0]) # time x-axis
self.ydata = np.array([0.0]) # fake data y-axis
# create a graph frame
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): # function looped every 0.2 second to read or produce data
"""
Loop starting with the simulation start, getting data and
pushing it to the graph every 0.2 seconds
"""
# concatenate data on the time x-axis
self.xdata = jk.core.add_datapoint(self.xdata, time.time(), xnptsmax=self.mygraph.xnptsmax)
# concatenate data on the fake data y-axis
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()