【问题标题】:Animating & overlaying multiple plots with plotly使用 plotly 动画和覆盖多个绘图
【发布时间】:2021-08-10 22:29:57
【问题描述】:

我有一个散点图和密度等高线,它们都是动画的,可以在输入数据帧通过步骤转换时对其进行可视化。我正在尝试将两个图叠加在一起。

scatter_fig = px.scatter(
        history_df,
        x='x',
        y='y',
        animation_frame="step",
        animation_group="entity_id",
        range_x=(0.0, 1.0),
        range_y=(0.0, 1.0),
        hover_data=hover_data,
        render_mode="webgl" if webgl else "svg",
        **color_kwargs
    )
contour_fig = px.density_contour(
        history_df,
        x='x',
        y='y',
        animation_frame="step",
        animation_group="entity_id",
        range_x=(0.0, 1.0),
        range_y=(0.0, 1.0),
        hover_data=None
    )

【问题讨论】:

    标签: python animation plotly


    【解决方案1】:
    • 你没有提供样本数据,所以我模拟了它
    • 概念很简单,取两个情节表达图形和框架/轨迹,并使用图形对象整合它们
    • 等值线图在 500 毫秒内未重绘,因此动画时间增加
    import pandas as pd
    import numpy as np
    import plotly.express as px
    import plotly.graph_objects as go
    
    # build sample data and define required variables
    history_df = pd.DataFrame(
        {
            "x": np.random.uniform(0, 1, 100),
            "y": np.random.uniform(0, 1, 100),
            "entity_id": np.random.choice(list("abcd"), 100),
        }
    )
    history_df = (
        history_df.groupby("entity_id", as_index=False)
        .apply(lambda d: d.reset_index(drop=True).assign(step=lambda dd: dd.index // 5))
        .reset_index(drop=True)
        .assign(step=lambda d: "step " + d["step"].astype(str))
    )
    hover_data = ["x", "y"]
    webgl = None
    color_kwargs = {}
    
    # original code
    scatter_fig = px.scatter(
        history_df,
        x="x",
        y="y",
        animation_frame="step",
        animation_group="entity_id",
        range_x=(0.0, 1.0),
        range_y=(0.0, 1.0),
        hover_data=hover_data,
        render_mode="webgl" if webgl else "svg",
        **color_kwargs
    )
    contour_fig = px.density_contour(
        history_df,
        x="x",
        y="y",
        animation_frame="step",
        animation_group="entity_id",
        range_x=(0.0, 1.0),
        range_y=(0.0, 1.0),
        hover_data=None,
    )
    
    # build frames to be animated from two source figures.  Each frame has 2 traces
    frames = [
        go.Frame(data=f.data + scatter_fig.frames[i].data, name=f.name)
        for i, f in enumerate(contour_fig.frames)
    ]
    
    # increase duration as contour takes a while to redraw
    # increase duration as contour takes a while to redraw
    updmenus = [{"args": [None, {"frame": {"duration": 2000}}],"label": "▶","method": "animate",},
                {'args': [[None], {'frame': {'duration': 0}, 'mode': 'immediate', 'fromcurrent': True, }],
                      'label': '◼', 'method': 'animate'} ]
    
    # now can animate...
    go.Figure(data=frames[0].data, frames=frames, layout=contour_fig.layout).update_layout(
        updatemenus=[{"buttons":updmenus}]
    )
    

    【讨论】:

    • 谢谢,它按预期工作,但在您的解决方案中,暂停按钮不再起作用。我该如何解决?
    • oops - 跳过了几个 updatemenus 参数。更新和修复
    • 如果我希望它们都具有不同的颜色怎么办?
    • @Epsi95 只需设置color_discrete_sequence=["red"] 或您想要的任何一个或两个px 初始图形创建调用。真正符合概念的简单性,创建然后集成
    猜你喜欢
    • 2021-09-07
    • 2015-12-21
    • 2020-01-19
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    相关资源
    最近更新 更多