首页 > 解决方案 > 使用 python seaborn 动画热图

问题描述

这真让我抓狂。我正在尝试创建热图的动画,其中每一帧都显示不同的时间戳。我已经创建了数据结构,导入了 ffmpeg,并且我也尝试了另一个包(看起来非常好的赛璐珞)。我尝试了一个带有简单线图的示例,它确实具有动画效果。结果总是一样的:我得到最后一帧,而不是动画。这是我的代码,以防任何人发现错误......提前谢谢你。

- - - - - - - 代码 - - - - - - -

from matplotlib import animation, rc
from IPython.display import HTML

data2 = df[['ProducerName', 'MemOccupied']]
data2 = data2.sort_index().sort_values('ProducerName', kind='mergesort')
data2 = data2[['MemOccupied']]
fig = plt.figure()
camera = Camera(fig)
df_heatmap = pd.DataFrame(data2)
df_heatmap = df_heatmap.dropna()
df_heatmap = df_heatmap.resample('1T').mean()
count = df_heatmap.count()
index_series = df_heatmap.index

def init():
    #plt.clf()
    ax = sns.heatmap(data2[data2.index == index_series[1]], cbar_kws={'label': 'Memory Occupied (GB)'}, cmap='Blues_r')
    ax.set(ylabel=None)
    #return (ax,)

def animate(i):
    plt.clf()
    ax = sns.heatmap(data2[data2.index == index_series[i]], cbar_kws={'label': 'Memory Occupied (GB)'}, cmap='Blues_r')
    ax.set(ylabel=None)
    #return (ax,)

anim = animation.FuncAnimation(fig = fig, func = animate, init_func=init, frames=count-1,repeat = True, interval = 200)

HTML(anim.to_html5_video())

标签: pythonpandasseabornheatmap

解决方案


推荐阅读