簡體   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