将pyplots保存到循环中的列表

2024-09-27 00:11:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个动画的以下情节,使它从左到右绘制

x = np.arange(1,10,1)
y = x * 2 - 1 + np.random.normal(0,1,9)
z = y / x
df = pd.DataFrame({'x':x, 'y':y, 'z':z})
df.set_index('x', drop=True, inplace=True)

fig, (ax1, ax2) = plt.subplots(nrows=2)
ax1.plot(df['y'], color='darkred')
ax2.bar(x=df.index, height=df['z'])
ax1.margins(0)
ax2.margins(0)

我正在寻找一个通用的解决方案,这样我可以创建1)一个以数据为输入的绘图函数,或2)一个具有绘图函数的类,并将1或2传递给我的LcAnimation类。下面给出了一个带有单个子图的图的示例。 如何最好地将plot函数传递给LcAnimation类(下面的代码适用于简单的plot,但不适用于多个子plot)

import matplotlib.animation as anim
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

class LcAnimation:
    def __init__(self, data, fig):
        '''
        :param data: A data frame containing the desired data for plotting
        :param fig: A matplotlib.pyplot figure on which the plot is drawn
        '''
        self.data = data.copy()
        self.fig = fig
        self.plots = []

    def create_animation(self, plot_class):
        '''
        :return: A list with the animated plots
        '''
        for i in range(0, len(self.data)):
            data_temp = self.data.loc[:i, ].copy()
            plt_im = plot_class.plot(data_temp)
            self.plots.append(plt_im)

    def save_animation(self, filename, fps = 10):
        '''

        :param filename: Destination at which the animation should be saved
        :return: Saves the animation
        '''
        animation_art = anim.ArtistAnimation(self.fig, self.plots)
        animation_art.save(filename, writer='pillow', fps=fps)

class MyPlot:

    def plot(self, data):
        plt_im = plt.plot(data['x'], data['y'], color='darkred')
        return plt_im


x = np.arange(1,10,1)
y = x * 2 - 1 + np.random.normal(0,1,9)
df = pd.DataFrame({'x':x, 'y':y})

fig = plt.figure()

# Test the module
my_plot = MyPlot()
MyFirstAnimation = LcAnimation(df, fig)
MyFirstAnimation.create_animation(my_plot)
MyFirstAnimation.save_animation('path', fps=5)

我希望动画能够处理多个子地块而不仅仅是1子地块的示例: Animation of y for each x


Tags: theimportselfdfdataplotdefas
1条回答
网友
1楼 · 发布于 2024-09-27 00:11:40

无需更改LcAnimation类中的任何内容,您只需在plotting函数中使用ax1ax2

x = np.arange(1,10,1)
y = x * 2 - 1 + np.random.normal(0,1,9)
z = y / x
df = pd.DataFrame({'x':x, 'y':y, 'z':z})
df.set_index('x', drop=True, inplace=True)

fig, (ax1, ax2) = plt.subplots(nrows=2)
ax1.margins(0)
ax2.margins(0)

class MyPlot:

    def plot(self, data):
        line, = ax1.plot(data['y'], color='darkred')
        bars = ax2.bar(x=data.index, height=data['z'], color='darkred')
        return [line,*bars]

# Test the module
my_plot = MyPlot()
MyFirstAnimation = LcAnimation(df, fig)
MyFirstAnimation.create_animation(my_plot)
MyFirstAnimation.save_animation('path.gif', fps=5)

enter image description here

相关问题 更多 >

    热门问题