0

我正在尝试为一系列 seaborn 热图制作动画并将结果从 Jupyter Notebook 导出到 mp4 文件,但似乎无法让我的代码正常工作。到目前为止,我有下面的代码创建了 mp4 文件,但它只显示了一个单一的热图,它闪烁到另一个并几乎立即停止,我在字典中有 100 多个数据帧,所有格式都像下面的虚拟数据片段一样我想要在文件中播放。到目前为止,这是我的代码:

%matplotlib inline

import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

Writer = animation.writers['ffmpeg']
writer = Writer(fps=20, metadata=dict(artist='Me'), bitrate=1800)

fig, ax = plt.subplots(figsize=(15, 10))
ax = sns.heatmap(heatmap_collection[0])
    
def init():
    #plt.clf()
    ax = sns.heatmap(np.random.normal(0, 1, (15, 10)))#, cbar=False)
   
    #ax = sns.heatmap(heatmap_collection[0], annot = True, fmt = '.1f', cmap='RdYlGn')
    #for t in ax.texts: t.set_text(t.get_text() + " %")
    #cbar = ax.collections[0].colorbar
    #cbar.set_ticks([0,.01, .02, .03])
    #cbar.set_ticklabels(['0%', '1%', '2%', '3%'])

def animate(i):
    plt.clf()
    ax = sns.heatmap(heatmap_collection[i], annot = True)

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=100, interval=5, repeat=True)
anim.save('data/animationTest.mp4', writer=writer)

我将热图数据存储在字典中,作为单独的数据帧,格式如下虚拟数据:

DummyData = pd.DataFrame(np.random.normal(0,1,size=(5, 4)), columns=list('FGHI'))
#df = df.set_index('A')
DummyData["Variant"] = [chr(ord('a') + x/26 - 1).upper() + chr(ord('a') + x%26).upper() 
                 if x >= 26
                 else chr(ord('a') + x).upper()
                 for x in DummyData.index]
DummyData = DummyData.set_index('Variant')

有人可以帮我指出正确的方向来完成这项工作吗?

4

0 回答 0