【问题标题】:Animations with Plotly with two parameters带有两个参数的 Plotly 动画
【发布时间】:2021-12-02 20:54:26
【问题描述】:

使用 Plotly,可以使用 Plotly Express 中的 animation_frame` 参数轻松创建动画,记录在 here。我想做同样的事情,但有两个参数(或者最终更多)。将伪代码添加到他们在我发布的链接中提供的示例中,我想做类似的事情:

import plotly.express as px
df = px.data.gapminder()
px.scatter(df, 
    x = "gdpPercap", 
    y = "lifeExp", 
    animation_frame_1 = "year", 
    animation_frame_2 = "another parameter"
)

并因此获得此图:

这可能吗?如何?我对动画功能(即随时间自动扫描参数)不感兴趣,只对让滑块探索数据感兴趣。

【问题讨论】:

    标签: python animation plotly


    【解决方案1】:
    • 概念
      1. 使用plotly express为每个动画帧创建一个图形
      2. 从第 1 步开始构建所有框架
      3. 构建布局,主要是步骤 1 中数字的滑块
      4. 最终从第 2 步和第 3 步构建一个新人物
    • 限制
      1. 空间...滑块空间很大
      2. 每个滑块都是独立的,最后选择的滑块就是显示的内容。没有视觉提示
    import plotly.express as px
    import plotly.graph_objects as go
    
    df = px.data.gapminder()
    # add another column to "animate" on...
    df["decade-continent"] = df.apply(
        lambda x: f'{round(x["year"], -1)}-{x["continent"]}', axis=1
    )
    
    # use px to do heavy lifting construction
    figs = [
        px.scatter(
            df,
            x="gdpPercap",
            y="lifeExp",
            animation_frame=ac,
        )
        # columns that become sliders
        for ac in ["continent", "year", "decade-continent"]
    ]
    
    # extract frames and sliders from each of the animated figures
    layout = figs[0].to_dict()["layout"]
    layout.pop("updatemenus") # don't want play and pause buttons
    layout["sliders"] = []
    frames = []
    for i, f in enumerate(figs):
        slider = f.to_dict()["layout"]["sliders"]
        slider[0]["y"] = -0.6 * i
        slider[0]["x"] = 0
        slider[0]["len"] = 1
    
        layout["sliders"] += slider
        frames += f.frames
    
    # finally build the figure with multiple sliders
    go.Figure(data=figs[0].data, frames=frames, layout=layout).update_layout(
        margin={"l": 0, "r": 0, "t": 0, "b": 0}, height=400
    )
    

    【讨论】:

      猜你喜欢
      • 2021-03-25
      • 2012-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      相关资源
      最近更新 更多