File size: 4,408 Bytes
0ab0788
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import plotly.express as px
import streamlit as st
import polars as pl
import pandas as pd

def plot_histogramme(df: pl.DataFrame, var, stat, stat_key, unit, height):
    df = df.to_pandas()
    # Définir l’ordre complet des mois
    month_order = ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin',
                   'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre']
    
    if stat_key == 'month':
        # Copie de sécurité
        df = df.copy()
        
        # Convertir le numéro du mois (1-12) en label texte
        df[var] = df[var].astype(int)
        df[var] = df[var].map({i+1: month_order[i] for i in range(12)})

        # Calculer la répartition (pourcentage) par mois
        counts = df[var].value_counts()                  # nb de lignes par mois présent
        counts = counts.reindex(month_order, fill_value=0)  # forcer l’existence de tous les mois, avec 0 pour les absents

        # Convertir en pourcentage
        total = counts.sum()
        freq_percent = (counts / total * 100) if total > 0 else counts
        
        # Construire un nouveau DF pour Plotly
        hist_df = pd.DataFrame({var: freq_percent.index, 'Pourcentage': freq_percent.values})

        # Plot en barres
        fig = px.bar(
            hist_df,
            x=var,
            y='Pourcentage'
        )
        fig.update_layout(
            bargap=0.1,           # Espacement entre barres
            xaxis_title="",       # Pas de titre horizontal
            yaxis_title="Pourcentage de stations",
            height=height,
            xaxis=dict(
                categoryorder='array',
                categoryarray=month_order
            )
        )
    else:
        # Cas normal : on garde px.histogram
        fig = px.histogram(
            df,
            x=var,
            nbins=50,
            histnorm='percent'
        )
        fig.update_layout(
            bargap=0.1,
            xaxis_title=f"{stat} ({unit})" if unit else f"{stat}",
            yaxis_title="Pourcentage de stations",
            height=height
        )
    return fig


def plot_histogramme_comparatif(df_observed: pl.DataFrame, df_modelised: pl.DataFrame, var, stat, stat_key, unit, height):
    df_observed = df_observed.to_pandas()
    df_modelised = df_modelised.to_pandas()
    month_order = ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin',
                   'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre']

    if stat_key == 'month':
        def prepare_df(df, label):
            df = df.copy()
            df[var] = df[var].astype(int)
            df[var] = df[var].map({i + 1: month_order[i] for i in range(12)})
            counts = df[var].value_counts()
            counts = counts.reindex(month_order, fill_value=0)
            total = counts.sum()
            freq_percent = (counts / total * 100) if total > 0 else counts
            return pd.DataFrame({
                var: freq_percent.index,
                'Pourcentage': freq_percent.values,
                'Source': label
            })

        df_obs = prepare_df(df_observed, "Observé")
        df_mod = prepare_df(df_modelised, "Modélisé")
        hist_df = pd.concat([df_obs, df_mod], ignore_index=True)

        fig = px.bar(
            hist_df,
            x=var,
            y='Pourcentage',
            color='Source',
            barmode='group'  # Affichage côte à côte
        )
        fig.update_layout(
            bargap=0.15,
            xaxis_title="",
            yaxis_title="Pourcentage de stations",
            height=height,
            xaxis=dict(
                categoryorder='array',
                categoryarray=month_order
            )
        )
    else:
        # Affichage standard pour les autres stats
        df_observed['Source'] = "Observé"
        df_modelised['Source'] = "Modélisé"
        df_all = pd.concat([df_observed, df_modelised], ignore_index=True)

        fig = px.histogram(
            df_all,
            x=var,
            color='Source',
            nbins=50,
            histnorm='percent',
            barmode='overlay'  # ou 'group' si tu veux les voir côte à côte
        )
        fig.update_layout(
            bargap=0.1,
            xaxis_title=f"{stat} ({unit})" if unit else f"{stat}",
            yaxis_title="Pourcentage de stations",
            height=height
        )

    return fig