Python matplotlib绘图/图形从起点部分刷新



对不起,我对python和matplotlib有点陌生,所以我不知道我问得是否正确。到目前为止,我正在绘制图形,收集通过串行端口来的整数数组,并立即刷新整个绘图区域。现在我想进行部分刷新(idk,如果它是正确的单词(,比如PPG/ECG轨迹,一旦线/轨迹到达绘图区域的末尾,它就会从头开始,就像这里的例子一样[1] :http://theblogofpeterchen.blogspot.com/2015/02/html5-high-performance-real-time.html.我确实知道,如果我继续添加串行端口数据,并在数据到达后立即绘制,我将继续向前扩展绘图,但我不知道如何返回起点并像心电图中那样逐渐重新绘制。

请在这方面提供帮助

谢谢

下面有一个使用FuncAnimation的解决方案。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from scipy.misc import electrocardiogram

ecg = electrocardiogram()[0:1000]
fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], '-')
# this is repeat length
n = 200

def init():
ax.set_xlim(0, n)
ax.set_ylim(-1, 2)
return ln,
def update(i):
# update xlim of axes
if i % n == 0:
ln.axes.set_xlim(int(i/n)*n, int(i/n)*n+n)
else:
xdata.append(i)
ydata.append(ecg[i])
ln.set_data(xdata, ydata)
return ln,
ani = FuncAnimation(fig, update, frames=1000, init_func=init, blit=True, interval=10, repeat=False)

最新更新