亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何強制 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 關注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號