【问题标题】:Add slider to plotly heatmap animation- python添加滑块以绘制热图动画-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)],
                  }],
    )

【问题讨论】:

    标签: python plotly plotly.graph-objects


    【解决方案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,
    
    )
    

    【讨论】:

      猜你喜欢
      • 2021-07-24
      • 1970-01-01
      • 2022-01-01
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      • 2017-06-17
      • 2016-05-10
      • 2015-12-07
      相关资源
      最近更新 更多