我想做一个动画的以下情节,使它从左到右绘制
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
无需更改LcAnimation类中的任何内容,您只需在plotting函数中使用
ax1
和ax2
相关问题 更多 >
编程相关推荐