如何使用 matplotlib FuncAnimation 顯示 numpy 數組的滑動窗口

聯邦

我正在開發一種用於檢測信號峰值的簡單算法。為了對我的算法進行故障排除(並展示它),我想在整個信號持續時間內(即=時間點的20分鐘觀察信號和檢測到的峰值100Hz20000

我認為最好的方法是創建一個動畫圖,matplotlib.animation.FuncAnimation它會連續顯示信號滑動 1 個時間點及其在5幾秒的時間窗口(即500時間點)內的疊加峰值信號存儲在 1D 中,numpy.ndarray而峰值信息存儲在numpy.ndarray包含峰值xy坐標的2D

這是情節如何的“靜止框架”。

在此處輸入圖片說明

現在的問題是我不能用 FuncAnimation 來解決這個問題。

If my understanding is correct I need three main pieces: the init_func parameter, a function that create the empty frame upon which the plot is drawn, the func parameter, that is the function that actually create the plot for each frame, and the parameter frames which is defined in the help as Source of data to pass func and each frame of the animation.

Looking at examples of plots with FuncAnimation, I can only find use-cases in which the data to plot are create on the go, like here, or here, where the data to plot are created on the basis of the frame.

What I do not understand is how to implement this with data that are already there, but that are sliced on the basis of the frame. I would thus need the frame to work as a sort of sliding window, in which the first window goes from 0 to 499, the second from 1to 500 and so on until the end of the time-points in the ndarray, and an associated func that will select the points to plot on the basis of those frames. I do not know how to implement this.

I add the code to create a realistic signal, to simply detect the peaks and to plot the 'static' version of the plot I would like to animate:

import neurokit2 as nk
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
from scipy.signal import find_peaks
#create realistic data
data = nk.ecg_simulate(duration = 50, sampling_rate = 100, noise = 0.05,\
                       random_state = 1)
#scale data
scaler = MinMaxScaler()
scaled_arr = scaler.fit_transform(data.reshape(-1,1))
#find peaks
peak = find_peaks(scaled_arr.squeeze(), height = .66,\
        distance = 60, prominence = .5)
#plot
plt.plot(scaled_arr[0:500])
plt.scatter(peak[0][peak[0] < 500],\
         peak[1]['peak_heights'][peak[0] < 500],\
             color = 'red')
r-beginners

我已經使用您提供的數據創建了一個動畫;我已經為 5000 個數據以 500 個增量提取數據並更新了圖表。為了便於提取數據,我創建了一個 500 行的索引,其中 id[0] 是開始行,id 1是結束行,幀數為 10。這段代碼有效,但初始行settings 和 dataset 在散點圖中不起作用,所以我在循環過程中直接繪製了散點圖。

import neurokit2 as nk
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from scipy.signal import find_peaks
import numpy as np

#create realistic data
data = nk.ecg_simulate(duration = 50, sampling_rate = 100, noise = 0.05, random_state = 1)

#scale data
scaler = MinMaxScaler()
scaled_arr = scaler.fit_transform(data.reshape(-1,1))
#find peaks
peak = find_peaks(scaled_arr.squeeze(), height = .66, distance = 60, prominence = .5)

ymin, ymax = min(scaled_arr), max(scaled_arr)
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot([],[], lw=2)
scat = ax.scatter([], [], s=20, facecolor='red')

idx = [(s,e) for s,e in zip(np.arange(0,len(scaled_arr), 1), np.arange(499,len(scaled_arr)+1, 1))]

def init():
    line.set_data([], []) 
    return line, 

def animate(i):
  id = idx[i]
  #print(id[0], id[1])
  line.set_data(np.arange(id[0], id[1]), scaled_arr[id[0]:id[1]])
  x = peak[0][(peak[0] > id[0]) & (peak[0] < id[1])]
  y = peak[1]['peak_heights'][(peak[0] > id[0]) & (peak[0] < id[1])]
  #scat.set_offsets(x, y)
  ax.scatter(x, y, s=20, c='red')
  ax.set_xlim(id[0], id[1])
  ax.set_ylim(ymin, ymax)
  return line,scat

anim = FuncAnimation(fig, animate, init_func=init, frames=50, interval=50, blit=True) 

plt.show()

在此處輸入圖片說明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 for 循環顯示數組

通過滑動窗口重複Numpy數組

如何使用循環顯示數組項?

如何为matplotlib FuncAnimation定义func参数

在matplotlib.animation.FuncAnimation()中使用Pymunk

React:隱藏和顯示使用 map() 顯示的數組中的特定元素

AttributeError:在matplotlib中使用FuncAnimation时,“ numpy.int32”对象没有属性“ get_zorder”

如何使用matplotlib FuncAnimation为热图制作动画?

animation.Funcanimation()Matplotlib /如何正确使用它?

如何在JS中顯示數組中的值?

android - 如何顯示存儲在 firestore 中的數組

React - 如何向數組添加新值並在警報窗口中顯示該數組?

如何使用css更改列表顯示中的列數?

Django如何將數組發送到JavaScript以動態顯示餅圖?

在軸 = 0 的 2D NumPy 數組上滑動窗口

Python Matplotlib FuncAnimation +保存

Matplotlib FuncAnimation用于散点图

如何使用for循環顯示數組中的第一個元素

如何使用二維數組顯示城鎮 C# 之間的距離

如何覆盖动画方法?matplotlib.animation.FuncAnimation

python Matplotlib gtk-使用FuncAnimation制作动画图

CSS 動畫:如何在向上滑動時顯示圖像?

如何在顫動中顯示圖像滑塊?

由於matplotlib,如何在同一圖中顯示具有重疊數據的多個圖形?

如何在 VSCode 中再次顯示新窗口

Wireshark:如何顯示數據包評論?

如何顯示倒數計時器

如何在 React 中顯示函數的結果?

如何顯示最小和最大除數?