From 2096bad84faed0c397a620a5dd9c12cb709db329 Mon Sep 17 00:00:00 2001 From: tamadei Date: Wed, 17 Apr 2024 14:17:50 +0000 Subject: [PATCH 1/2] funtion to plot podium given data --- utils/viz.py | 56 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/utils/viz.py b/utils/viz.py index b1dfab2..7b725e9 100644 --- a/utils/viz.py +++ b/utils/viz.py @@ -5,19 +5,21 @@ from IPython.display import display, Markdown + def graphePerso(prenom, nom, data, titre): - densite=stat.dens(data['duration'], bins = stat.idealBins(len(data['duration']))) - fcubic=stat.lissage(densite, sep = True,beginend = (data['duration'].min(),data['duration'].max())) - z=data.index[data.Athlète==(nom.upper()+" "+prenom.capitalize())] - temps = data.loc[z[0],'duration'] - x= np.linspace(data['duration'].min(),data['duration'].max(), int(1e5)) + densite = stat.dens(data['duration'], bins=stat.idealBins(len(data['duration']))) + fcubic = stat.lissage(densite, sep=True, beginend=(data['duration'].min(), + data['duration'].max())) + z = data.index[data['Athlète'] == (nom.upper() + " " + prenom.capitalize())] + temps = data.loc[z[0], 'duration'] + x = np.linspace(data['duration'].min(), data['duration'].max(), int(1e5)) fig = go.Figure() fig.add_trace(go.Scatter( x=x, y=fcubic(x), fill='tozeroy', - mode='lines', + mode='lines', line_color='blue', name='densité lissée & interpolée' )) @@ -41,9 +43,49 @@ def graphePerso(prenom, nom, data, titre): fig.show() + def display_header(header): display(Markdown(f"**Compétition:** {header['nom']}")) display(Markdown(f"**Lieu:** {header['lieu']}")) display(Markdown(f"**Date:** {header['date']}")) display(Markdown(f"**Dept:** {header['dept']}")) - display(Markdown(f"**Label:** {header['label']}")) \ No newline at end of file + display(Markdown(f"**Label:** {header['label']}")) + + +def display_podium(data): + data.h_duration = pd.to_datetime(data.h_duration, format='%H:%M:%S').dt.time + podium = data.sort_values(by='h_duration').iloc[[2, 0, 1]] + + fig = go.Figure() + + fig.add_trace(go.Bar( + y=[1, 3, 2], + x=podium['Athlète'], + orientation='v', + marker=dict( + color=['#614e1a', '#a57c00', '#c7d1da'], # Couleur du podium (bronze, gold, silver) + line=dict(color='black', width=1) # Couleur de la bordure + ) + )) + + for i, annotation_text in enumerate(['🥉', '🥇', '🥈']): + fig.add_annotation( + x=podium.iloc[i]['Athlète'], + y=[1.2, 3.2, 2.2][i], + text=annotation_text, + showarrow=False, + font=dict(size=70) + ) + + fig.update_layout( + title="Podium", + yaxis_title="Durée", + yaxis=dict( + tickvals=[1, 2, 3], + ticktext=podium['h_duration'].sort_values(ascending=False) + ), + xaxis=dict( + tickfont=dict(size=16) + ) + ) + fig.show() From db639c907b676fcfd32eeb9753e55c0281b2719d Mon Sep 17 00:00:00 2001 From: tamadei Date: Wed, 17 Apr 2024 15:31:25 +0000 Subject: [PATCH 2/2] plot personal times with cdf, not density of all times --- utils/viz.py | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/utils/viz.py b/utils/viz.py index 7b725e9..692bccf 100644 --- a/utils/viz.py +++ b/utils/viz.py @@ -1,36 +1,61 @@ import plotly.graph_objects as go import numpy as np import pandas as pd -import utils.stat as stat from IPython.display import display, Markdown -def graphePerso(prenom, nom, data, titre): - densite = stat.dens(data['duration'], bins=stat.idealBins(len(data['duration']))) - fcubic = stat.lissage(densite, sep=True, beginend=(data['duration'].min(), - data['duration'].max())) +def graphePerso(prenom, nom, data, titre, nb_bins=10_000): + times_in_seconds = pd.to_datetime(data.h_duration, format='%H:%M:%S').dt.time.apply( + lambda time: 3600*time.hour+60*time.minute+time.second) + bins = np.linspace(times_in_seconds.iloc[0], times_in_seconds.iloc[-1], nb_bins) + cdf = [(times_in_seconds <= bin_).sum()/len(times_in_seconds) for bin_ in bins] + z = data.index[data['Athlète'] == (nom.upper() + " " + prenom.capitalize())] temps = data.loc[z[0], 'duration'] - x = np.linspace(data['duration'].min(), data['duration'].max(), int(1e5)) + cdf_proportion = (times_in_seconds <= temps).sum() / len(times_in_seconds) fig = go.Figure() fig.add_trace(go.Scatter( - x=x, y=fcubic(x), + x=bins, y=cdf, fill='tozeroy', mode='lines', line_color='blue', - name='densité lissée & interpolée' + name='Distribution des temps' )) fig.add_trace(go.Scatter( - x=[temps, temps], y=[0, max(fcubic(x))], + x=[temps, temps], y=[0, max(cdf)], mode='lines', line_color='red', name=nom.upper()+" "+prenom.capitalize() )) + fig.add_shape( + type="line", + x0=bins[0], + y0=cdf_proportion, + x1=temps, + y1=cdf_proportion, + line=dict( + color="black", + width=1, + dash="dashdot", + ), + ) + + fig.add_annotation( + x=temps, + y=cdf_proportion, + text=f"{100*cdf_proportion:.2f}%", + font=dict(size=14), + showarrow=True, + arrowhead=7, + ax=0, + ay=-30, + ) + fig.update_layout( title=titre, xaxis_title="Durée pour franchir la ligne d'arrivée",