gc_plots/duration_comparison.py
2024-09-19 20:13:12 +02:00

59 lines
2 KiB
Python

try:
from gc_wrapper.GC_Wrapper import GC_wrapper as GC
except ImportError:
pass
import pathlib
import plotly
import plotly.graph_objs as go
import tempfile
import pandas as pd
# Define temporary file
temp_file = tempfile.NamedTemporaryFile(mode="w+t", prefix="GC_", suffix=".html", delete=False)
def get_time_moving(season_metrics):
df = pd.DataFrame(season_metrics)[['date', 'Time_Moving']]
# sum up moving times for days with multiple activities
df = df.groupby(['date']).sum()
df = df.cumsum()
df = pd.DataFrame({'date': df.index, 'hours': df['Time_Moving'] / 3600.0})
start_date = df['date'][0]
df['days_since'] = df['date'].apply(lambda x: (x - start_date).days)
return df
def cumulated_time_trace(season_metrics, color, name):
df = get_time_moving(season_metrics)
return go.Scatter(x=df['days_since'], y=df['hours'], line={'color': color}, name=name)
def main():
season_metrics_all = GC.seasonMetrics(compare=True)
seasons_info = GC.season(compare=True)
fig = go.Figure()
for season, name in zip(season_metrics_all, seasons_info['name']):
color = season[1]
season_metrics = season[0]
trace = cumulated_time_trace(season_metrics, color, name)
fig.add_trace(trace)
fig.update_layout(
title="Duration Comparison",
paper_bgcolor='rgba(0, 0, 0, 0)',
plot_bgcolor='rgba(0, 0, 0, 0)',
showlegend=True,
xaxis=dict(showline=True, mirror=True, ticks='inside'),
yaxis=dict(showline=True, mirror=True, ticks='inside'),
xaxis_title='Days',
yaxis_title='Time (h)',
)
fig.update_xaxes(showline=True, linewidth=1, linecolor='black', mirror=True)
fig.update_yaxes(showline=True, linewidth=1, linecolor='black', mirror=True, showgrid=True, gridcolor='lightgray')
plotly.offline.plot(fig, auto_open=False, filename=temp_file.name)
GC.webpage(pathlib.Path(temp_file.name).as_uri())
if __name__ == "__main__":
main()