如何使用 matplotlib 对多个数组进行动画处理
我试图在数组序列上显示某种绘图刷新。每个数组都是序列的一个实例,对于每个数组,我想绘制相对数组及其平滑版本,如下所示: 由于动画功能,这些帧应该按顺序显示 无论如何,当我从动画函数返回相对行时,会出现以下错误“ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需要进行微小的改变。主要是,您必须为动画循环解压 Line2D 对象
Only minor changes are necessary. Mainly, you have to unpack the Line2D objects for the animation loop