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