How to create a heatmap animation with plotly express?

前端 未结 1 478
野的像风
野的像风 2021-01-06 17:36

I have a list of square matrices, M[t], where t ranges from 0 to N and I wish to create an animated heatplot using plotly.express. The entries in each row/column correspond

相关标签:
1条回答
  • 2021-01-06 18:26

    Does this work for you?

    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()
    

    UPDATE In case you need to add a Pause button

    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()
    
    0 讨论(0)
提交回复
热议问题