带有两个参数的 Plotly 动画

Posted

技术标签:

【中文标题】带有两个参数的 Plotly 动画【英文标题】:Animations with Plotly with two parameters 【发布时间】: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"
)

并因此获得此图:

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

【问题讨论】:

【参考方案1】: 概念
    使用plotly express为每个动画帧创建一个图形 从第 1 步开始构建所有框架 构建布局,主要是步骤 1 中数字的滑块 最终从第 2 步和第 3 步构建一个新人物
限制
    空间...滑块空间很大 每个滑块都是独立的,最后选择的滑块就是显示的内容。没有视觉提示
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
)

【讨论】:

以上是关于带有两个参数的 Plotly 动画的主要内容,如果未能解决你的问题,请参考以下文章

R语言plotly可视化:使用plotly可视化对比不同参数设置下的同一机器学习模型算法的拟合曲线(训练两个参数不同的KNN模型进行对比comparing different kNN models

将静态色阶设置为 Plotly Heatmap 动画

Plotly:如何创建一个没有标题的表格?

js动画效果

R语言plotly可视化:plotly可视化箱图相同数据集对比使用不同分位数算法的可视化差异(quartilemethod参数linearinclusiveexclusive)

Plotly:如何使用 plotly express 在单迹散点图中显示图例?