将滑块添加到 plotly 热图动画 - python

Add slider to plotly heatmap animation- python

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

一个 b
一个 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)],
                  }],
    )
  • 已生成与您描述的内容相对应的数据帧列表
  • go.Frames() 构造函数和 sliders 中定义 argsname 的关键使用]
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,

)