【问题标题】:How to make jupyter HTML-matplotlib animation with seaborn heatmap?如何使用 seaborn 热图制作 jupyter HTML-matplotlib 动画?
【发布时间】:2018-12-24 20:50:54
【问题描述】:

我尝试使用 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

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

【问题讨论】:

    标签: python matplotlib animation jupyter seaborn


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2019-10-21
      • 2018-11-23
      • 2017-09-29
      • 2020-09-22
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多