Python 线图在动画功能中正确更新,散点图最初绘制,但根本不更新

Python 线图在动画功能中正确更新,散点图最初绘制,但根本不更新,python,animation,plot,real-time,scatter,Python,Animation,Plot,Real Time,Scatter,我正在接受实时心电数据(目前正在模拟)并处理该数据,然后实时绘制它。我的线图正在成功地绘制和更新,而经过太多小时的搜索(不幸的是,我是一个初学者),我终于能够获得散点图数据进行初始绘制,但它并不像线图那样更新。我怀疑是因为我没有像绘制直线图那样使用np.linspace命令设置散点图,也不知道如何在data25集上使用extend,这是我创建的集,其中x(峰值列表)和y(ybeat)是实时散点图 import heartpy as hp import numpy as np import mat

我正在接受实时心电数据(目前正在模拟)并处理该数据,然后实时绘制它。我的线图正在成功地绘制和更新,而经过太多小时的搜索(不幸的是,我是一个初学者),我终于能够获得散点图数据进行初始绘制,但它并不像线图那样更新。我怀疑是因为我没有像绘制直线图那样使用np.linspace命令设置散点图,也不知道如何在data25集上使用extend,这是我创建的集,其中x(峰值列表)和y(ybeat)是实时散点图

import heartpy as hp
import numpy as np
import matplotlib.pyplot as plt

import matplotlib.animation as animation 
from collections import deque
from matplotlib.ticker import FuncFormatter
#from . import config 





sample_rate = 40
def init(): # initialization function: plot the background of each frame
   line.set_ydata([np.nan]*len(x))
   line1.set_ydata([np.nan]*len(x1))
  # scatter2.set_xdata([np.nan]*len(x2))
   # scatter3.set_ydata([np.nan]*len(x3))
   # scatter4.set_ydata([np.nan]*len(x4))
  # scatter5.set_ydata([np.nan]*len(y5))
   scat.set_offsets([]*len(x))


   return line, line1, scat, #scatter5, #scatter4, scatter5

def animate(i):
    read_fname = 'temp.1D'
    datax = np.loadtxt('temp.1D')
    wd, m = hp.process(datax, 40 ) #run analysis        
    hr = wd['hr'] 
    rm = wd['rolling_mean'] 
    yb = wd['ybeat']
    pl = wd['peaklist']
    rp = wd['removed_beats']
    rp_y = wd['removed_beats_y']
    #pl=ax.collections[pl].get_array()

    yhr = np.array(hr[:])
    yrm = np.array(rm[:])
    yyb = np.array(yb[:])
    ypl = np.array(pl[:])
    yrp = np.array(rp[:])
    yrp_y = np.array(rp_y[:])
    yhr = np.concatenate([yhr])
    yrm = np.concatenate([yrm])
    yyb = np.concatenate([yyb])
    ypl = np.concatenate([ypl])
    yrp = np.concatenate([rp])
    yrp_y = np.concatenate([rp_y])

    data.extend(yhr)
    data1.extend(yrm) 
    data2.extend(ypl)
  #  data3.extend(yrp)
   # data4.extend(yrp_y)
    data5.extend(yyb)
    #data25.extend(ypl,yyb)

    line.set_ydata(data)
    line1.set_ydata(data1)
   # scatter2.set_xdata(data2)
   # scatter3.set_ydata(data3)
    # scatter4.set_ydata(data4)
   # scatter5.set_ydata(data5)
    data25 =(np.hstack((ypl[:i,np.newaxis],yyb[:i,np.newaxis]))) 
    scat.set_offsets(data25)




    print(yhr)
    print(yrm)
    print(ypl)


    return line, line1, scat, #scatter5, #scatter4, scatter5

max_x = 500
max_y = 1700
#max_x2 = 1000
#max_y5 = 1000
data = deque(np.zeros(max_x),maxlen=max_x)
data1 = deque(np.zeros(max_x),maxlen=max_x)
data2= deque(np.zeros(max_x),maxlen=max_x)
#data3 = deque(np.zeros(max_x),maxlen=max_x)
#data4 = deque(np.zeros(max_x),maxlen=max_x) #hold the last 10 values
data5 = deque(np.zeros(max_y),maxlen=max_y)
data25 = deque(np.zeros(max_x),maxlen=max_x)

x = np.arange(0,max_x)
x1 = np.arange(0,max_x)   
x2 = np.arange(0,max_x)
#x3 = np.arange(0,max_x)
#x4 = np.arange(0,max_x)
y5 = np.arange(0,max_y)

fig, ax = plt.subplots()
#fig, ax1 = plt.subplots()
ax.set_ylim(-500, max_y)
ax.set_xlim(0, max_x-1)
line, = ax.plot(x,  np.linspace(0,1000,max_x,endpoint = False))
line1, = ax.plot(x1, np.linspace(0,1000,max_x,endpoint = False))
scat = ax.scatter([], [],)
#ax = plt.gca()
#scatter2, = ax.plot(x2, marker="o", ls="", color = "red")
#scatter3, = ax.scatter(x3, np.linspace(0,50,1000))
#scatter4 = ax.scatter(x4, np.linspace(0,50,1000))
#scatter5, = ax.plot(y5, marker="o", ls="", color = "green")

#ax.grid()
#ax.relim()
#ax.autoscale_view(True,True,True)

ax.xaxis.set_major_formatter(FuncFormatter(lambda x, pos: '{:.0}s'.format(max_x -x -990)))

plt.xlabel('Seconds ago')



ani = animation.FuncAnimation(fig,animate,init_func=init,interval=25,blit=True,save_count = 10)




plt.show()