gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:20:03 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com