seaborn动画热图/相关矩阵

r s*_*zle 9 python animation matplotlib heatmap seaborn

我是python的新手(来自matlab).作为一个项目,我试图创建一个随时间变化的相关矩阵的动画图.为了让情节变得美好,我正在尝试海盗.我很难完成动画制作(在mac上遇到matplotlib后端问题),但现在使用来自网络的代码可以使用一个非常基本的动画:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

nx = 50
ny = 50

fig = plt.figure()
data = np.random.rand(nx, ny)
im = plt.imshow(data)

def init():
    im.set_data(np.zeros((nx, ny)))

def animate(i):
    #xi = i // ny
    #yi = i % ny
    data = np.random.rand(nx, ny)
    im.set_data(data)
    return im

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=50, repeat = False)
Run Code Online (Sandbox Code Playgroud)

现在我试图将其改编为seaborn,但没有成功.似乎seaborn在次要情节上工作并且使这些动画变得更加困难.我得到的最好的东西是一种递归情节,其中seaborn.heatmaps被绘制在彼此之上.此外,im.set_data方法不可用.任何建议都非常感谢.非常感谢你.

最好的,拉尔夫.

r s*_*zle 8

我将plt.imshow(通过强制转换数据set_data不起作用)替换为seaborn.heatmap.

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

fig = plt.figure()
data = np.random.rand(10, 10)
sns.heatmap(data, vmax=.8, square=True)

def init():
      sns.heatmap(np.zeros((10, 10)), vmax=.8, square=True, cbar=False)

def animate(i):
    data = np.random.rand(10, 10)
    sns.heatmap(data, vmax=.8, square=True, cbar=False)

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=20, repeat = False)
Run Code Online (Sandbox Code Playgroud)

这创建了我苦苦挣扎的递归情节。

  • 我想我刚刚找到了一个解决方案:将 plt.clf() 放在动画函数的第一行现在可以工作!谢谢,拉尔夫。 (3认同)
  • 如何在 Jupyter Notebook 中实现此功能? (2认同)