添加滑块以绘制热图动画-python

Posted

技术标签:

【中文标题】添加滑块以绘制热图动画-python【英文标题】:Add slider to plotly heatmap animation- python 【发布时间】:2021-10-10 11:51:39 【问题描述】:

我想为热图动画添加一个滑块。我有五个不同的数据帧(每个数据帧一帧)。数据框如下:

a b
a 530 300
b NaN 200
c NaN 100
d 100 444

每一帧实际上都是时间数据。为简单起见,我使用了计数。到目前为止,这是我的代码。动画有效,播放和暂停按钮也有效。我能够创建一个滑块,但它不起作用。我错过了什么吗?有人可以帮忙吗?

  # Convert the dictionaries to dataframes
    df = 
    frames = 0
    for i in caller_callees:
        df[i] = pd.DataFrame(dict[i], dtype=int).T
        frames += 1

    fig = go.Figure(
        data=[go.Heatmap(z=df[0].values, x=df[0].columns, y=df[0].index)],
        layout=go.Layout(
            # autosize=True,
            height=800,
            yaxis="title": 'callers',
            xaxis="title": 'callees', "tickangle": 45, 'side': 'top',
            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=df[i])],
                         layout=go.Layout(title_text=f"Frame i"))
                for i in range(0, frames)]
    )

    # finally create the slider
    fig.update_layout(
        sliders=["steps": ["args": [
                                        [f],
                                        "frame": "duration": 0, "redraw": False,
                                         "mode": "immediate",
                                         "transition": "duration": 300
                                         ,
                                    ],
                             "label": f, "method": "animate", 
                            for f in range(0, frames)],
                  ],
    )

【问题讨论】:

【参考方案1】: 生成的与您描述的内容相对应的数据框列表 在定义 args 时在 go.Frames() 构造函数和 sliders 中使用 name
import pandas as pd
import numpy as np
import plotly.graph_objects as go

dfs = [pd.DataFrame(index=list("abcd"), columns=list("ab"),
                    data=np.where(np.random.randint(1, 8, [4, 2]) == 1,
                                  np.nan, np.random.randint(1, 500, [4, 2]),)
                   )
       for i in range(10)]

# generate the frames. NB name
frames = [
    go.Frame(data=go.Heatmap(z=df.values, x=df.columns, y=df.index), name=i)
    for i, df in enumerate(dfs)
]

go.Figure(data=frames[0].data, frames=frames).update_layout(
    updatemenus=[
        
            "buttons": ["args": [None, "frame": "duration": 500, "redraw": True],
                         "label": "Play", "method": "animate",,
                        "args": [[None],"frame": "duration": 0, "redraw": False,
                                          "mode": "immediate", "transition": "duration": 0,,],
                         "label": "Pause", "method": "animate",,],
            "type": "buttons",
        
    ],
    # iterate over frames to generate steps... NB frame name...
    sliders=["steps": ["args": [[f.name],"frame": "duration": 0, "redraw": True,
                                            "mode": "immediate",,],
                         "label": f.name, "method": "animate",
                        for f in frames],],
    height=800,
    yaxis="title": 'callers',
    xaxis="title": 'callees', "tickangle": 45, 'side': 'top',
    title_x=0.5,

)

【讨论】:

以上是关于添加滑块以绘制热图动画-python的主要内容,如果未能解决你的问题,请参考以下文章

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

Plotly:如何更改动画热图中的过渡时间?

是否可以向 for 循环创建的滑块添加动画?

将动态文本动画添加到 nivo 滑块

如何在Python中添加动画以绘制图形

使用canvas实现图片滑动验证