2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

plotlyでアニメーションするグラフを作ったメモ

Last updated at Posted at 2020-04-09

Plotlyでアニメーションするグラフを作ったメモ

概要

  • np.arrayを入力するとアニメーションするグラフを作成する(時系列データ)

ソースコード

import pandas as pd
import numpy as np
import plotly.express as px

def create_animation_graph(input_array: np.array, start: str = '1900-01-01 0:00', g_type: str = 'line', y_label: str = 'value',
                           x_label: str = 'time') -> object:
    """
    np.arrayを入力すると、plotlyのアニメーションするグラフを返す
    :param input_array:データのnp.array
    :param start:開始時刻
    :param g_type:グラフタイプ 'line','bar','area','scatter'
    :param y_label:y軸名称
    :param x_label:x軸名称
    :return:
    """
    df = pd.DataFrame()
    times = pd.date_range(start=start, periods=len(input_array), freq='T').strftime('%H:%M')

    # animation用のデータフレームを作成
    for idx, time in enumerate(list(times)):
        add_df = pd.DataFrame({x_label: [time for time in times [0:idx]],
                               y_label: input_array [0:idx],
                               'tick': idx})
        df = pd.concat([df, add_df])

    range_x_max = len(input_array) - 2
    
    # plotly.expressで描画
    if g_type == 'scatter':
        fig = px.scatter(df, 
                         x=x_label, 
                         y=y_label,
                         animation_frame="tick",
                         range_y=[0, input_array.max()],
                         range_x=[0, range_x_max])
    elif g_type == 'line':
        fig = px.line(df, 
                      x=x_label, 
                      y=y_label, 
                      animation_frame="tick", 
                      range_y=[0, input_array.max()],
                      range_x=[0, range_x_max])
    elif g_type == 'bar':
        fig = px.bar(df, 
                     x=x_label, 
                     y=y_label, 
                     animation_frame="tick", 
                     range_y=[0, input_array.max()],
                     range_x=[0, range_x_max])
    elif g_type == 'area':
        fig = px.area(df, 
                      x=x_label, 
                      y=y_label, 
                      animation_frame="tick", 
                      range_y=[0, input_array.max()],
                      range_x=[0, range_x_max])
    else:
        raise KeyError('g_typeが違います')

    return fig

実行

input_array = np.array([_*np.random.rand() for _ in  np.arange(0,30)*2])
create_animation_graph(input_array,g_type='area',start='00:00',x_label='時刻',y_label='電力量')

animation.gif

2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?