Python 实时绘图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24783530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python realtime plotting
提问by CoMartel
I acquire some data in two arrays: one for the time, and one for the value. When I reach 1000 points, I trigger a signal and plot these points (x=time, y=value).
我在两个数组中获取一些数据:一个是时间,一个是值。当我达到 1000 点时,我触发一个信号并绘制这些点(x=时间,y=值)。
I need to keep on the same figure the previous plots, but only a reasonable number to avoid slowing down the process. For example, I would like to keep 10,000 points on my graph. The matplotlib interactive plot works fine, but I don't know how to erase the first points and it slows my computer very quickly. I looked into matplotlib.animation, but it only seems to repeat the same plot, and not really actualise it.
我需要保持与之前的图相同的数字,但只是一个合理的数字,以避免减慢过程。例如,我想在我的图表上保留 10,000 个点。matplotlib 交互式绘图工作正常,但我不知道如何擦除第一个点,它会很快降低我的计算机速度。我查看了 matplotlib.animation,但它似乎只是重复了相同的情节,并没有真正实现它。
I'm really looking for a light solution, to avoid any slowing.
我真的在寻找一个简单的解决方案,以避免任何放缓。
As I acquire for a very large amount of time, I erase the input data on every loop (the 1001st point is stored in the 1st row and so on).
由于我采集了很长时间,我在每个循环中删除了输入数据(第 1001 个点存储在第 1 行中,依此类推)。
Here is what I have for now, but it keeps all the points on the graph:
这是我现在所拥有的,但它保留了图表上的所有点:
import matplotlib.pyplot as plt
def init_plot():
plt.ion()
plt.figure()
plt.title("Test d\'acqusition", fontsize=20)
plt.xlabel("Temps(s)", fontsize=20)
plt.ylabel("Tension (V)", fontsize=20)
plt.grid(True)
def continuous_plot(x, fx, x2, fx2):
plt.plot(x, fx, 'bo', markersize=1)
plt.plot(x2, fx2, 'ro', markersize=1)
plt.draw()
I call the init function once, and the continous_plot is in a process, called every time I have 1000 points in my array.
我调用了 init 函数一次,continous_plot 正在一个进程中,每次我的数组中有 1000 个点时都会调用。
采纳答案by DrV
The lightest solution you may have is to replace the X and Y values of an existing plot. (Or the Y value only, if your X data does not change. A simple example:
您可能拥有的最简单的解决方案是替换现有绘图的 X 和 Y 值。(或者只有 Y 值,如果你的 X 数据没有改变。一个简单的例子:
import matplotlib.pyplot as plt
import numpy as np
import time
fig = plt.figure()
ax = fig.add_subplot(111)
# some X and Y data
x = np.arange(10000)
y = np.random.randn(10000)
li, = ax.plot(x, y)
# draw and show it
ax.relim()
ax.autoscale_view(True,True,True)
fig.canvas.draw()
plt.show(block=False)
# loop to update the data
while True:
try:
y[:-10] = y[10:]
y[-10:] = np.random.randn(10)
# set the new data
li.set_ydata(y)
fig.canvas.draw()
time.sleep(0.01)
except KeyboardInterrupt:
break
This solution is quite fast, as well. The maximum speed of the above code is 100 redraws per second (limited by the time.sleep
), I get around 70-80, which means that one redraw takes around 4 ms. But YMMV depending on the backend, etc.
这个解决方案也非常快。上面代码的最大速度是每秒 100 次重绘(受 限制time.sleep
),我得到大约 70-80,这意味着一次重绘大约需要 4 毫秒。但是 YMMV 取决于后端等。
回答by f.rodrigues
Use a fixed size array and plot that using matplot.
使用固定大小的数组并使用 matplot 绘制该数组。
import collections
array = collections.deque([None] * 1000, maxlen=1000)
Whenver you append to the array it will remove the first element.
每当您附加到数组时,它都会删除第一个元素。
回答by Guillaume S
I know I'm late to answer this question, bt for your issue you could look into the "joystick" package. It is based on the line.set_data() and canvas.draw() methods, with optional axes re-scaling. It also allows for interactive text logging or image plotting (in addition to graph plotting). No need to do your own loops in a separate thread, the package takes care of it, just give the update frequency you wish. Plus the console remains available for additional monitoring commands. See http://www.github.com/ceyzeriat/joystick/or https://pypi.python.org/pypi/joystick(use pip install joystick to install)
我知道我回答这个问题迟到了,但是对于您的问题,您可以查看“操纵杆”包。它基于 line.set_data() 和 canvas.draw() 方法,具有可选的轴重新缩放。它还允许交互式文本记录或图像绘图(除了图形绘图)。无需在单独的线程中执行您自己的循环,包会处理它,只需提供您希望的更新频率。此外,控制台仍可用于其他监控命令。参见http://www.github.com/ceyzeriat/joystick/或https://pypi.python.org/pypi/joystick(使用 pip install joystick 安装)
try:
尝试:
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()
回答by Javier Guzman
To be totally interactive, you could use Bokeh for this. Concretely you could use an update function that is called every X ms and stream the new data.
为了完全互动,您可以使用 Bokeh。具体来说,您可以使用每 X 毫秒调用一次的更新函数并流式传输新数据。
Here there is a fragment I use:
这里有一个我使用的片段:
def update():
candle_data.stream(new_data, 300)
plot = figure(x_axis_type='datetime',x_range=(start_day, final_day), width=1500, height=900, title='Live Chart', sizing_mode='scale_both')
plot.segment(x0='time', y0='highest', x1='time', y1='lowest', color='black', source=candle_data)
plot.vbar(x='time', width = 0.5*60*60*50 ,bottom='open', top='close',fill_color='color', line_color='black', source = candle_data)
doc.add_root(column([plot]))
doc.add_periodic_callback(update, 20000)
doc.title = "Candle Data Live Rates"