繁体   English   中英

如何为seaborn的热图或相关矩阵设置动画?

[英]How to animate a seaborn's heatmap or correlation matrix?

我对python比较陌生(来自Matlab)。 作为一个项目,我正在尝试随时间创建相关矩阵的动画图。 为了使情节好看,我正在尝试seaborn。 我努力完成动画(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)

现在,我试图将其调整为seaborn ,但没有成功。 似乎 seaborn 在子情节上工作,并且为这些子情节制作动画要困难得多。 我曾经得到的最好的东西是一种递归图,其中seaborn.heatmaps被绘制在彼此之上。 此外, im.set_data方法不可用。

任何建议都非常感谢。

我换成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)

这创建了我挣扎的递归图。

这是一个完整的示例(使用 Matplotlib 3.0.3 测试)。

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


def animate_heat_map():
    fig = plt.figure()

    nx = ny = 20
    data = np.random.rand(nx, ny)
    ax = sns.heatmap(data, vmin=0, vmax=1)

    def init():
        plt.clf()
        ax = sns.heatmap(data, vmin=0, vmax=1)

    def animate(i):
        plt.clf()
        data = np.random.rand(nx, ny)
        ax = sns.heatmap(data, vmin=0, vmax=1)

    anim = animation.FuncAnimation(fig, animate, init_func=init, interval=1000)

    plt.show()


if __name__ == "__main__":
    animate_heat_map()

根据 r schmaelzle 的回答,我创建了带有注释的动画 seaborn 热图。

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


class Heatmap:

    def __init__(self):
        self.fig, self.ax = plt.subplots()
        self.anim = None

    def animate(self):
        def init():
            sns.heatmap(np.zeros((10, 10)), vmax=.8, ax=self.ax)

        def animate(i):
            self.ax.texts = []
            sns.heatmap(np.random.rand(10, 10), annot=True, vmax=.8, cbar=False, ax=self.ax)

        self.anim = animation.FuncAnimation(self.fig, animate, init_func=init, frames=20, repeat=False)

if __name__ == '__main__':
    hm = Heatmap()
    hm.animate()

更新注释的技巧是使ax.texts = []为空。

我希望它能帮助别人! :)

除了你上面的答案,我想从数据框列表中做到这一点并保存为 gif。 因此,使用您的代码和 Serenity 对Matplotlib 动画迭代的回答熊猫数据帧列表

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

def animate(i):
    data = data_list[i]
    sns.heatmap(data, vmax=.8, square=True, cbar=False)

data_list = []
for j in range(20):
    data = np.random.rand(10, 10)
    data_list.append(data)

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=20, repeat = False)

savefile = r"test3.gif"
pillowwriter = animation.PillowWriter(fps=20)
anim.save(savefile, writer=pillowwriter)

plt.show()

谢谢!!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM