python - 有没有办法在 matplotlib 中使用相同的图形导出一些动画?
问题描述
我有一些基于相同 plt.figure() 的动画.FuncAnimation,我想将它们导出到 matplotlib 中的一个动画(视频)以改善图形可视化。
我已经在互联网上做过这方面的研究,但我没有发现任何有用的东西。
self.ax, self.voxels = None, None
self.fig = plt.figure()
# Creating the Animation object
lenght_1 = 10
lenght_2 = 15
self.ani = animation.FuncAnimation(self.fig, self.update, lenght_1, interval=650, blit=False, repeat=True)
self.ani1 = animation.FuncAnimation(self.fig, self.update_2nd, lenght_2, interval=650, blit=False, repeat=True)
def update(self, num):
"""
The function which update the plot for every frame
:param num: iterations
:return: plot animation
"""
self.ax.cla()
elev = self.elevators[0]
point = elev.shortest_path
floor, row, col = point[num]
self.facecolors[row][col][floor] = '#ff99ff'
self.fcolors_2 = self.explode(self.facecolors)
self.voxels = self.ax.voxels(self.x, self.y, self.z, self.filled_2,
facecolors=self.fcolors_2, edgecolors=self.ecolors_2)
if [floor, row, col] == elev.DESTINATION:
self.facecolors[row][col][floor] = '#ff99ff4D'
elif [row, col] != ElevatorConst.SHAFT_DESC and [row, col] != ElevatorConst.SHAFT_ASC:
self.facecolors[row][col][floor] = '#1f77b430'
else:
if [row, col] == ElevatorConst.SHAFT_DESC:
color = '#00140d33'
self.facecolors[row][col][floor] = color
else:
color = '#ffffff33'
self.facecolors[row][col][floor] = color
def update_2nd(self, num):
"""
The function which update the plot for every frame
:param num: iterations
:return: plot animation
"""
self.ax.cla()
elev = self.elevators[1]
point = elev.shortest_path
floor, row, col = point[num]
self.facecolors[row][col][floor] = '#49fdb8'
self.fcolors_2 = self.explode(self.facecolors)
self.voxels = self.ax.voxels(self.x, self.y, self.z, self.filled_2,
facecolors=self.fcolors_2, edgecolors=self.ecolors_2)
if [floor, row, col] == elev.DESTINATION:
self.facecolors[row][col][floor] = '#49fdb84D'
elif [row, col] != ElevatorConst.SHAFT_DESC and [row, col] != ElevatorConst.SHAFT_ASC:
self.facecolors[row][col][floor] = '#1f77b430'
else:
if [row, col] == ElevatorConst.SHAFT_DESC:
color = '#00140d33'
self.facecolors[row][col][floor] = color
else:
color = '#ffffff33'
self.facecolors[row][col][floor] = color
新的更新功能:
def try_update(self, num):
"""
The function which update the plot for every frame
:param num: iterations
:return: plot animation
"""
for elev in self.elevators:
self.ax.cla()
point = elev.shortest_path
if num < len(point):
floor, row, col = point[num]
self.facecolors[row][col][floor] = ElevatorColors.ELEVATOR[elev.id]
self.fcolors_2 = self.explode(self.facecolors)
self.voxels = self.ax.voxels(self.x, self.y, self.z, self.filled_2,
facecolors=self.fcolors_2, edgecolors=self.ecolors_2)
if [floor, row, col] == elev.DESTINATION:
self.facecolors[row][col][floor] = ElevatorColors.DESTINATION[elev.id]
elif [row, col] != ElevatorConst.SHAFT_DESC and [row, col] != ElevatorConst.SHAFT_ASC:
self.facecolors[row][col][floor] = ElevatorColors.PATH
else:
if [row, col] == ElevatorConst.SHAFT_DESC:
color = ElevatorColors.SHAFT_DESC
self.facecolors[row][col][floor] = color
else:
color = ElevatorColors.SHAFT_ASC
self.facecolors[row][col][floor] = color
最后,我想导出显示我的代码可视化的动画(视频)。
解决方案
我仍然无法运行问题中的代码,所以我所能做的就是展示这个概念。而不是两个FuncAnimation
相互竞争的s,一个需要坚持一个动画。
def update_1(num):
# code for the first animation
def update_2(num):
# code for the second animation
def update_all(num):
ax.clear()
if num < max1:
update_1(num)
else:
update_1(max1)
# the second animation is assumed to always take `num`
update_2(num)
ani = FuncAnimation(fig, update_all, frames=max2)
推荐阅读
- django - NoReverseMatch at /1/stationary-item-1/ 为“addreview”反向,未找到任何参数
- javascript - 如何替换所有出现的数组?
- c - C中char数组的奇怪输出行为
- android - Kotlin:访问数据类的属性时 sortedBy { it.field } 出现 IllegalAccessError
- javascript - event.preventDefault() 仅在第一次有效
- sql - CMD.ExecuteScalar() 在存储过程中返回 0 以插入数据
- c++ - AllocatorAwareContainer 和复制和交换习语
- python - 从列表中获取值
- java - 无法使用 XMLAdapter 解组 ZoneDateTime(引发错误)
- c++ - 关于三角表而不是三角函数的性能问题