簡體   English   中英

如何使用 plotly express 創建熱圖 animation?

[英]How to create a heatmap animation with plotly express?

我有一個方陣列表 M[t],其中 t 的范圍從 0 到 N,我希望使用 plotly.express 創建一個動畫熱圖。 每行/列中的條目對應一個列表,a=['a1','a2',...'aN']

animation 上的 plotly 文檔相當稀疏,僅關注散點圖和條形圖

https://plotly.com/python/animations/

與我類似的問題已發布在

如何在 Plotly 中為熱圖制作動畫

但是,用戶正在使用 Jupyter 筆記本。 我只是在Mac(OS 10.15.4)上使用帶有IDLE的Python 3.7

我知道如何使用ZF1C25ED1C25ED1BBF9DEE9BE5092BZ使用ZF0211321137A5A5FFF03E3EEB4664640Z或ZD50F42A37901F21D0A21D69D63C5BNEND BUCTING 34C5B7B7B7B7B7B7B7F03C54SPAR。 這是我使用的一種方法,但我確信使用 matplotlib.animation 有更有效的方法:

import numpy as np
import matplotlib.pyplot as plt
#50 matrices, each of size 4-by-4.
N = 50
M = np.random.random((50, 4,4))

#Desired labels for heatmap--not sure where to put.
labels=['a','b','c','d']

fig, ax = plt.subplots()

for t in range(50):
    ax.cla()
    ax.imshow(M[t])
    ax.set_title("frame {}".format(t))
    plt.pause(0.1)

這對你有用嗎?

import numpy as np
import plotly.graph_objs as go

N = 50
M = np.random.random((N, 10, 10))

fig = go.Figure(
    data=[go.Heatmap(z=M[0])],
    layout=go.Layout(
        title="Frame 0",
        updatemenus=[dict(
            type="buttons",
            buttons=[dict(label="Play",
                          method="animate",
                          args=[None])])]
    ),
    frames=[go.Frame(data=[go.Heatmap(z=M[i])],
                     layout=go.Layout(title_text=f"Frame {i}")) 
            for i in range(1, N)]
)

fig.show()

更新如果您需要添加Pause按鈕

fig = go.Figure(
    data=[go.Heatmap(z=M[0])],
    layout=go.Layout(
        title="Frame 0",
        title_x=0.5,
        updatemenus=[dict(
            type="buttons",
            buttons=[dict(label="Play",
                          method="animate",
                          args=[None]),
                    dict(label="Pause",
                         method="animate",
                         args=[None,
                               {"frame": {"duration": 0, "redraw": False},
                                "mode": "immediate",
                                "transition": {"duration": 0}}],
                         )])]
    ),
    frames=[go.Frame(data=[go.Heatmap(z=M[i])],
                     layout=go.Layout(title_text=f"Frame {i}")) 
            for i in range(1, N)]
)

fig.show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM