首页 > 解决方案 > 有没有办法在 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

最后,我想导出显示我的代码可视化的动画(视频)。

标签: pythonmatplotlibanimationfigure

解决方案


我仍然无法运行问题中的代码,所以我所能做的就是展示这个概念。而不是两个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)

推荐阅读