繁体   English   中英

如何使用 plotly express 为 plot 中的线设置动画?

[英]How to animate line in scatter plot using plotly express?

考虑分散的官方示例 plot animation

import plotly.express as px
df = px.data.gapminder()
px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
           size="pop", color="continent", hover_name="country",
           log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90])

这导致 plot 中的标记正在动画化。 但是,我也想为连接线设置动画,这是不可能直接实现的。 我尝试更新fig.frames.data[n].mode=markers+lines之类的framesmodemarker属性,但仍然没有成功。 有人可以让我知道我哪里出错了。

干杯,DD

如果您通过animate the connecting lines表示连接,例如,上面示例中所有说明Asia等的标记,那么将以下内容添加到您的代码中将产生下面的 plot。

fig.for_each_trace(lambda t: t.update(mode = 'lines+markers'))
for fr in fig.frames:
    for d in fr.data:
        d.update(mode='markers+lines')

Plot:

在此处输入图像描述

完整代码:

import plotly.express as px
df = px.data.gapminder()
fig = px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
           size="pop", color="continent", hover_name="country",
           log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90],
           # mode = 'markers+lines'
           height = 600, width = 1000
          )

# lineas and markers on first display
fig.for_each_trace(lambda t: t.update(mode = 'lines+markers'))

# lineas and markers on animation frames
for fr in fig.frames:
    for d in fr.data:
        d.update(mode='markers+lines')
        
fig.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM