如何使用 matplotlib 对多个数组进行动画处理

发布于 2025-01-15 16:44:18 字数 1936 浏览 2 评论 0原文

我试图在数组序列上显示某种绘图刷新。每个数组都是序列的一个实例,对于每个数组,我想绘制相对数组及其平滑版本,如下所示: 由于动画功能,这些帧应该按顺序显示 无论如何,当我从动画函数返回相对行时,会出现以下错误“ AttributeError: 'list' object has no attribute 'get_zorder' ”。我尝试了其他问题和渠道的解决方案,但没有任何效果。这是代码:

import random

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
from scipy.signal import savgol_filter

n_instances = 1000  # the instances that will be generated
instances_duration = 100  # the duration of each of them

# starting the data matrix
process_instances = np.zeros((n_instances, instances_duration))
# starting the filtered data matrix
filtered_instances = np.zeros((n_instances, instances_duration))

np.random.seed(2)
for i in range(n_instances):
    # creating the instance as a random array
    current_instance = np.random.normal(0, 1, instances_duration)
    # assigning to the relative matrix
    process_instances[i, :] = current_instance
    # filtering and assigning to the relative matrix
    filtered_instances[i, :] = savgol_filter(current_instance, 11, 3)

# managing the plots
fig, axs = plt.subplots()
axs.set_ylim([-3, 3])
axs.set_xlim([0, instances_duration])
axs.grid(True)
lines = axs.plot(process_instances[0, :], alpha=0.3, label='original')  # starting the main lines
lines_filt = axs.plot(filtered_instances[0, :], label='filtered')  # starting the filtered lines
axs.legend()


def animate(frame):
    # updating lines
    for columns, line in enumerate(lines):
        line.set_ydata(process_instances[frame, :])
    # updating filtered lines
    for columns, line in enumerate(lines_filt):
        line.set_ydata(filtered_instances[frame, :])
    print("Showing frame number: " + str(frame))
    return [lines, lines_filt]


animation = FuncAnimation(fig, animate, interval=1000, blit=True, repeat=True)
animation.event_source.start()
plt.show()

I am attempting to show a sort of plot refresh on a sequence of array. Each array is an instance of the sequence and for each of them I want to plot the relative array, plus its smoothed version, like that:
these frames should be showed in sequence thanks to the animation function
Anyway the following error " AttributeError: 'list' object has no attribute 'get_zorder' " arises when I return the relative lines from the animation function. I tried solutions from other questions and channels but nothing really worked. Here is the code:

import random

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
from scipy.signal import savgol_filter

n_instances = 1000  # the instances that will be generated
instances_duration = 100  # the duration of each of them

# starting the data matrix
process_instances = np.zeros((n_instances, instances_duration))
# starting the filtered data matrix
filtered_instances = np.zeros((n_instances, instances_duration))

np.random.seed(2)
for i in range(n_instances):
    # creating the instance as a random array
    current_instance = np.random.normal(0, 1, instances_duration)
    # assigning to the relative matrix
    process_instances[i, :] = current_instance
    # filtering and assigning to the relative matrix
    filtered_instances[i, :] = savgol_filter(current_instance, 11, 3)

# managing the plots
fig, axs = plt.subplots()
axs.set_ylim([-3, 3])
axs.set_xlim([0, instances_duration])
axs.grid(True)
lines = axs.plot(process_instances[0, :], alpha=0.3, label='original')  # starting the main lines
lines_filt = axs.plot(filtered_instances[0, :], label='filtered')  # starting the filtered lines
axs.legend()


def animate(frame):
    # updating lines
    for columns, line in enumerate(lines):
        line.set_ydata(process_instances[frame, :])
    # updating filtered lines
    for columns, line in enumerate(lines_filt):
        line.set_ydata(filtered_instances[frame, :])
    print("Showing frame number: " + str(frame))
    return [lines, lines_filt]


animation = FuncAnimation(fig, animate, interval=1000, blit=True, repeat=True)
animation.event_source.start()
plt.show()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗琴音 2025-01-22 16:44:18

只需要进行微小的改变。主要是,您必须为动画循环解压 Line2D 对象

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
from scipy.signal import savgol_filter

n_instances = 1000  # the instances that will be generated
instances_duration = 100  # the duration of each of them

# starting the data matrix
process_instances = np.zeros((n_instances, instances_duration))
# starting the filtered data matrix
filtered_instances = np.zeros((n_instances, instances_duration))

np.random.seed(2)
for i in range(n_instances):
    # creating the instance as a random array
    current_instance = np.random.normal(0, 1, instances_duration)
    # assigning to the relative matrix
    process_instances[i, :] = current_instance
    # filtering and assigning to the relative matrix
    filtered_instances[i, :] = savgol_filter(current_instance, 11, 3)

# managing the plots
fig, axs = plt.subplots()
axs.set_ylim([-3, 3])
axs.set_xlim([0, instances_duration])
axs.grid(True)
#unpack the Line2D artists
lines, = axs.plot(process_instances[0, :], alpha=0.3, label='original')  # starting the main lines
lines_filt, = axs.plot(filtered_instances[0, :], label='filtered')  # starting the filtered lines
axs.legend()


def animate(frame):
    # updating lines
    lines.set_ydata(process_instances[frame, :])
    # updating filtered lines
    lines_filt.set_ydata(filtered_instances[frame, :])
    print("Showing frame number: " + str(frame))
    
    #return the Line2D artists for blitting
    return lines, lines_filt,


animation = FuncAnimation(fig, animate, interval=1000, blit=True, repeat=True)
animation.event_source.start()
plt.show()

Only minor changes are necessary. Mainly, you have to unpack the Line2D objects for the animation loop

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
from scipy.signal import savgol_filter

n_instances = 1000  # the instances that will be generated
instances_duration = 100  # the duration of each of them

# starting the data matrix
process_instances = np.zeros((n_instances, instances_duration))
# starting the filtered data matrix
filtered_instances = np.zeros((n_instances, instances_duration))

np.random.seed(2)
for i in range(n_instances):
    # creating the instance as a random array
    current_instance = np.random.normal(0, 1, instances_duration)
    # assigning to the relative matrix
    process_instances[i, :] = current_instance
    # filtering and assigning to the relative matrix
    filtered_instances[i, :] = savgol_filter(current_instance, 11, 3)

# managing the plots
fig, axs = plt.subplots()
axs.set_ylim([-3, 3])
axs.set_xlim([0, instances_duration])
axs.grid(True)
#unpack the Line2D artists
lines, = axs.plot(process_instances[0, :], alpha=0.3, label='original')  # starting the main lines
lines_filt, = axs.plot(filtered_instances[0, :], label='filtered')  # starting the filtered lines
axs.legend()


def animate(frame):
    # updating lines
    lines.set_ydata(process_instances[frame, :])
    # updating filtered lines
    lines_filt.set_ydata(filtered_instances[frame, :])
    print("Showing frame number: " + str(frame))
    
    #return the Line2D artists for blitting
    return lines, lines_filt,


animation = FuncAnimation(fig, animate, interval=1000, blit=True, repeat=True)
animation.event_source.start()
plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文