为了账号安全,请及时绑定邮箱和手机立即绑定

如何强制 Plotly 动画从最后一帧开始?

如何强制 Plotly 动画从最后一帧开始?

qq_笑_17 2023-10-06 18:43:35
我想更改动画图表的“默认”框架,并强制它在渲染 Dash 应用程序时使用最后一个日期作为默认值。我怎样才能做到这一点?import plotly.express as pxdf = 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])我正在尝试一些不同的方法,例如:fig.layout['sliders'][0]['active'] = 11andframe = -1fig = go.Figure(fig.frames[frame].data, fig.frames[frame].layout)fig但它没有正确反映在图表上。有人可以指导我如何找到有关它的参考资料吗?我想强制图表从去年开始。
查看完整描述

2 回答

?
阿晨1998

TA贡献2037条经验 获得超6个赞

非常感谢兄弟的回答;有用。


我构建了一个与您的解决方案类似但有一些差异的解决方案:


import plotly.express as px

import plotly.graph_objects as go


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])


last_frame_num = len(fig.frames) -1


fig.layout['sliders'][0]['active'] = last_frame_num


fig = go.Figure(data=fig['frames'][-1]['data'], frames=fig['frames'], layout=fig.layout)


fig

不管怎样,我非常感谢您的关注/支持,在实施某些事情时有更多的选择真是太好了!


非常感谢; 最好的问候,莱昂纳多


查看完整回答
反对 回复 2023-10-06
?
函数式编程

TA贡献1807条经验 获得超9个赞

问题是fig.data你有第一帧。我找到了生成新图形的解决方法。


import plotly.express as px

import plotly.graph_objects as go


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])


# New figure

fig2 = go.Figure()


# add last frame traces to fig2

for tr in fig.frames[-1].data:

    fig2.add_trace(tr)


# copy the layout

fig2.layout = fig.layout


#  copy the frames

fig2.frames = fig.frames


# set last frame as the active one

fig2.layout['sliders'][0]['active'] = len(fig.frames) - 1


fig2

https://img1.sycdn.imooc.com//651fe516000134c406540417.jpg

查看完整回答
反对 回复 2023-10-06
  • 2 回答
  • 0 关注
  • 144 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号