LoginSignup
0
0

More than 1 year has passed since last update.

Python>Plotly:フレーム間を補間するアニメーションを除去する方法

Last updated at Posted at 2022-02-16

1.概要

Plotlyで時間経過をグラフ化すると自動的にフレーム間を補完するアニメーションが入ります。
それをなくす方法を調べました。

2.デフォルトのグラフ(フレーム間のアニメーションあり)

以下のサイトのコードをベースに考えます。

通常設定では以下のようにフレーム間を補間するアニメーションがついています。

input
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])
fig.show()

02_01_default_duration500_.html-個人-Microsoft_-Edge-2022-02-16-23-53-11.gif

3.フレーム間アニメーション除去バージョンしたグラフ

上で作成したfigの設定内容を変更することで補間アニメーションを除去できます。

3.1.フレームの設定の確認

以下コマンドを実行するとフレームの表示処理に関する設定を確認できます。

input
fig.layout.updatemenus
output
(layout.Updatemenu({
     'buttons': [{'args': [None, {'frame': {'duration': 500, 'redraw': False},
                  'mode': 'immediate', 'fromcurrent': True, 
                  'transition':{'duration': 500, 'easing': 'linear'}}],
                  'label': '▶',
                  'method': 'animate'},
                 {'args': [[None], {'frame': {'duration': 0, 'redraw': False},
                  'mode': 'immediate', 'fromcurrent': True, 
                  'transition':{'duration': 0, 'easing': 'linear'}}],
                  'label': '◼',
                  'method': 'animate'}],
     'direction': 'left',
     'pad': {'r': 10, 't': 70},
     'showactive': False,
     'type': 'buttons',
     'x': 0.1,
     'xanchor': 'right',
     'y': 0,
     'yanchor': 'top'
 }),)

ここで4行目の'transition':{'duration': 500, 'easing': 'linear'}}],の500は
フレーム間の補間アニメーションで500msつかうことを示しています。  

参考
https://github.com/plotly/plotly.py/blob/03979d105c65dda3df3a155322eaff18f203b03f/packages/python/plotly/plotly/validators/layout/_transition.py

3.1.フレームの設定の変更

4行目の'transition':{'duration': 500, 'easing': 'linear'}}],の500を0に変更することで
フレーム間の補間アニメーションをなくすことができます。

input
fig.update_layout(updatemenus = [{
    'buttons': [{'args': [None, {'frame': {'duration': 500, 'redraw': False},
                                 'mode': 'immediate', 'fromcurrent': True, 
                                 'transition':{'duration': 0, 'easing': 'linear'}}],#'duration': 500-> 0
                 'label': '▶',
                 'method': 'animate'},
                {'args': [[None], {'frame': {'duration': 0, 'redraw': False},
                                   'mode': 'immediate', 'fromcurrent': True, 'transition':
                                   {'duration': 0, 'easing': 'linear'}}],
                 'label': '◼',
                 'method': 'animate'}],
    'direction': 'left',
    'pad': {'r': 10, 't': 70},
    'showactive': False,
    'type': 'buttons',
    'x': 0.1,
    'xanchor': 'right',
    'y': 0,
    'yanchor': 'top'
 }]) 

02_01_no_animation_duration0_.html-個人-Microsoft_-Edge-2022-02-16-23-54-02.gif

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0