【问题标题】:Python matplotlib plot /graph partial refresh from starting pointPython matplotlib plot /graph从起点部分刷新
【发布时间】:2020-09-02 20:32:07
【问题描述】:

对不起,我对 python 和 matplotlib 有点陌生,所以我不知道我问的是否正确。截至目前,我正在绘制图表,在其中收集通过串行端口传入的整数数组并立即刷新整个绘图区域。现在我想做部分刷新(idk,如果它是正确的词)类似于 PPG/ECG 跟踪,一旦线/跟踪到达绘图区域的末尾,它就会从头开始,就像这里的例子一样 [1]:http://theblogofpeterchen.blogspot.com/2015/02/html5-high-performance-real-time.html。 我确实明白,如果我继续附加串行端口数据并在它到达后立即绘制它将继续向前扩展绘图,但我不知道如何从起点返回并逐渐重绘它,就像在 ECG 中一样。

请在这方面提供帮助

谢谢

【问题讨论】:

  • 请提供MCVE和玩具示例数据,我认为matplotlib动画可以做到。

标签: python matplotlib plot


【解决方案1】:

我有一个使用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)

【讨论】:

  • 您好,感谢您的帮助。您的示例有很大帮助,但是一旦跟踪到达绘图区域的右侧极端,它也会清除绘图,这是不可取的,而链接 theblogofpeterchen.blogspot.com/2015/02/… 中的演示会清除并更新与横坐标相对应的纵坐标。我不知道我是否解释正确。请在链接中播放演示
  • 我们强烈建议您根据我提供的代码让事情变得更好,而不仅仅是要求一个完整的解决方案,这不是 SO 的工作方式。如果你不认为这是一个解决方案,不接受它是完全可以的。
  • 对不起,我打算接受它但等待更多帮助;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-01
  • 1970-01-01
相关资源
最近更新 更多