簡體   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