【问题标题】:Generating animation from a list of pandas dataframe从熊猫数据框列表生成动画
【发布时间】:2017-05-18 16:40:32
【问题描述】:

我有一个 pandas 数据帧列表,其中的数据帧代表进程中的后续帧。因此,数据帧的大小和结构都相同(相同的列和索引)。我想知道是否有办法为这些数据帧设置动画并将动画保存为 mpeg 电影。

我尝试了以下操作,但是没有任何运气:

#X: a list of 1000 dataframe, all of same size 
fig = plt.figure()
ax = fig.add_subplot(111)

im = ax.imshow(X,interpolation='nearest')


def update_img(n):

    im.set_data(n.values)
    return im

animation = animation.FuncAnimation(fig,update_img, frames = X, interval=30)

上面卡在第一帧。

【问题讨论】:

  • 问题中的代码运行良好。您可能需要提供minimal reproducible example 并明确说明您运行它的方式和位置以及您使用的库版本。

标签: python animation matplotlib


【解决方案1】:

要将动画保存为mp4,可以使用

import matplotlib.pyplot as plt
import matplotlib.animation
import numpy as np; np.random.seed(1)
import pandas as pd

X = [pd.DataFrame(np.random.rand(10,10)) for i in range(100)] 
fig = plt.figure()
ax = fig.add_subplot(111)

im = ax.imshow(X[0],interpolation='nearest')

def update_img(n):
    im.set_data(n.values)

ani = matplotlib.animation.FuncAnimation(fig,update_img, frames = X, interval=30)

FFwriter = matplotlib.animation.FFMpegWriter(fps=30)     
ani.save(__file__+".mp4",writer = FFwriter)

plt.show()

【讨论】:

  • 谢谢!在“im = ax.imshow(X[0],interpolation='nearest')”中,我通过了列表 X。我认为这是问题的原因。再次感谢!
猜你喜欢
  • 1970-01-01
  • 2017-11-06
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
  • 1970-01-01
  • 1970-01-01
  • 2015-07-31
相关资源
最近更新 更多