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.Wenn Sie ein Array eingeben, wird ein grafisch animiertes Diagramm zurückgegeben
:param input_array:Daten np.array
:param start:Startzeit
:param g_type:Diagrammtyp'line','bar','area','scatter'
:param y_label:Name der y-Achse
:param x_label:Name der x-Achse
:return:
"""
df = pd.DataFrame()
times = pd.date_range(start=start, periods=len(input_array), freq='T').strftime('%H:%M')
#Erstellen Sie einen Datenrahmen für die 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.Zeichnen Sie mit 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_Typ ist anders')
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='Tageszeiten',y_label='Elektrische Energie')
Recommended Posts