python - 如何使用seaborn热图制作jupyter HTML-matplotlib动画?

标签 python matplotlib animation jupyter seaborn

我尝试使用 Seaborn 热图在 jupyter 中制作 HTML(anim.to_html5_video) 动画。

  • 首先,我从文档中获取工作示例,并制作“纯 matplotlib”image map animated example ,它工作了,有一个小问题(动画单元中的“寄生输出”)
  • 然后,我尝试make it work with seaborn.heatmap ……但是失败了。动画看起来像“无限镜子”——显然 matplotlib 轴/绘图组合有问题,但我无法理解。

通用初始化单元:

import pandas as pd
import seaborn as sns
import numpy as np
%matplotlib inline
#%matplotlib notebook # Tried both, not needed for animation.
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from IPython.display import HTML

动画有效,但“存在不需要的静态输出图像”:

fig, ax = plt.subplots()
nx = 50
ny = 50

line2d, = ax.plot([], [], lw=2)

def init():
    line2d.set_data([], [])
    ax.imshow(np.zeros((nx, ny)))
    return (line2d,)

def animate(i):
    data = np.random.rand(nx, ny)
    ax.set_title('i: ' + str(i))    
    ax.imshow(data)
    return (line2d,)

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=10, interval=1000, blit=False)

HTML(anim.to_html5_video())

所以,看起来我的 jupyter 设置(包、ffmpeg 等)一切正常。

但是,我不知道如何用seaborn.heatmap制作它:

fig, ax = plt.subplots()
nx = 50
ny = 50

line2d, = ax.plot([], [], lw=2)

ax_global = ax

def init_heatmap():
    line2d.set_data([], [])
    sns.heatmap(np.zeros((nx, ny)), ax=ax_global)
    return (line2d,)


def animate_heatmap(i):
    data = np.random.rand(nx, ny)
    sns.heatmap(data, ax=ax_global)
    ax.set_title('Frame: ' + str(i))    

    return (line2d,)

anim = animation.FuncAnimation(fig, animate_heatmap, init_func=init_heatmap,
                              frames=10, interval=1000, blit=True)

HTML(anim.to_html5_video())    

两个样本均已准备好test on github

当然,我想看带有随机 map 和“稳定热轴”的动画 但得到这个 https://vimeo.com/298786185/

最佳答案

您可以切换“颜色栏”。来自 Seaborn.heatmap documentation ,您需要将 sns.heatmap(data, ax=ax_global) 更改为 sns.heatmap(data, ax=ax_global, cbar=False) 并在内部执行相同操作init_heatmap()

关于python - 如何使用seaborn热图制作jupyter HTML-matplotlib动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53917596/

相关文章:

python - 合并时间戳大于或等于的数据帧

Matplotlib 的 Figure 和 Axes 解释

matplotlib - Jupyter 笔记本 : automatically saves all images

ios - 如何在图像中添加快速 Spring 动画?

ios - 在当前 View Controller 下呈现新的 View Controller

python - NLTK 的所有可能的 pos 标签是什么?

javascript - Django 尝试将我的数组保存到模型中

python - 读取.dat文件日期字符串

python - 热图中的标签组

javascript - Snap.svg 延迟动画不起作用