diff --git a/__pycache__/utils_graph.cpython-312.pyc b/__pycache__/utils_graph.cpython-312.pyc index 764d9f4d7ce68a27ea9b5961014d09a63ac6f7d0..4a0e69a1eae5ce45a8f2be1e5dff6310d90176fc 100644 Binary files a/__pycache__/utils_graph.cpython-312.pyc and b/__pycache__/utils_graph.cpython-312.pyc differ diff --git a/app.py b/app.py index 8ee249a2cae03507866ece6de4b6527f28c98843..bc8921ef53af451711d4218346eaa5b4a8ff2eab 100644 --- a/app.py +++ b/app.py @@ -8,11 +8,15 @@ from io import BytesIO import utils.utils_graph as utils_graph from utils.utils_graph import plot_graph -from onglets import onglet1 +from onglets import onglet1, onglet2 st.title('Logiciel cartographique') option = st.sidebar.selectbox("Choisir le format du graphique :", ("Graphique en barre horizontale","Histogramme")) if option == "Graphique en barre horizontale" : - onglet1.display() \ No newline at end of file + onglet1.display() + +if option == "Histogramme" : + onglet2.display() + diff --git a/data/data_generate.ipynb b/data/data_generate.ipynb index b04aa650ad709e076f16727cc88f8ca170dfb176..0e56755dc63e04aa28a41134a97236809b5795da 100644 --- a/data/data_generate.ipynb +++ b/data/data_generate.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -21,7 +21,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -151,7 +151,7 @@ "[100 rows x 4 columns]" ] }, - "execution_count": 5, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -159,11 +159,148 @@ "source": [ "df" ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "<div>\n", + "<style scoped>\n", + " .dataframe tbody tr th:only-of-type {\n", + " vertical-align: middle;\n", + " }\n", + "\n", + " .dataframe tbody tr th {\n", + " vertical-align: top;\n", + " }\n", + "\n", + " .dataframe thead th {\n", + " text-align: right;\n", + " }\n", + "</style>\n", + "<table border=\"1\" class=\"dataframe\">\n", + " <thead>\n", + " <tr style=\"text-align: right;\">\n", + " <th></th>\n", + " <th>denomination_cfa</th>\n", + " <th>count</th>\n", + " <th>numero_color</th>\n", + " <th>name_legend</th>\n", + " </tr>\n", + " </thead>\n", + " <tbody>\n", + " <tr>\n", + " <th>0</th>\n", + " <td>CFA_1</td>\n", + " <td>44</td>\n", + " <td>0</td>\n", + " <td>couleur 1</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1</th>\n", + " <td>CFA_2</td>\n", + " <td>47</td>\n", + " <td>1</td>\n", + " <td>couleur 2</td>\n", + " </tr>\n", + " <tr>\n", + " <th>2</th>\n", + " <td>CFA_3</td>\n", + " <td>64</td>\n", + " <td>1</td>\n", + " <td>couleur 2</td>\n", + " </tr>\n", + " <tr>\n", + " <th>3</th>\n", + " <td>CFA_4</td>\n", + " <td>67</td>\n", + " <td>3</td>\n", + " <td>couleur 4</td>\n", + " </tr>\n", + " <tr>\n", + " <th>4</th>\n", + " <td>CFA_5</td>\n", + " <td>67</td>\n", + " <td>0</td>\n", + " <td>couleur 1</td>\n", + " </tr>\n", + " <tr>\n", + " <th>5</th>\n", + " <td>CFA_6</td>\n", + " <td>9</td>\n", + " <td>0</td>\n", + " <td>couleur 1</td>\n", + " </tr>\n", + " <tr>\n", + " <th>6</th>\n", + " <td>CFA_7</td>\n", + " <td>83</td>\n", + " <td>1</td>\n", + " <td>couleur 2</td>\n", + " </tr>\n", + " <tr>\n", + " <th>7</th>\n", + " <td>CFA_8</td>\n", + " <td>21</td>\n", + " <td>2</td>\n", + " <td>couleur 3</td>\n", + " </tr>\n", + " <tr>\n", + " <th>8</th>\n", + " <td>CFA_9</td>\n", + " <td>36</td>\n", + " <td>4</td>\n", + " <td>couleur 5</td>\n", + " </tr>\n", + " <tr>\n", + " <th>9</th>\n", + " <td>CFA_10</td>\n", + " <td>87</td>\n", + " <td>2</td>\n", + " <td>couleur 3</td>\n", + " </tr>\n", + " </tbody>\n", + "</table>\n", + "</div>" + ], + "text/plain": [ + " denomination_cfa count numero_color name_legend\n", + "0 CFA_1 44 0 couleur 1\n", + "1 CFA_2 47 1 couleur 2\n", + "2 CFA_3 64 1 couleur 2\n", + "3 CFA_4 67 3 couleur 4\n", + "4 CFA_5 67 0 couleur 1\n", + "5 CFA_6 9 0 couleur 1\n", + "6 CFA_7 83 1 couleur 2\n", + "7 CFA_8 21 2 couleur 3\n", + "8 CFA_9 36 4 couleur 5\n", + "9 CFA_10 87 2 couleur 3" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.head(10)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "streamlit_env", + "display_name": "base", "language": "python", "name": "python3" }, @@ -177,7 +314,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.16" + "version": "3.12.7" } }, "nbformat": 4, diff --git a/onglets/__pycache__/onglet1.cpython-312.pyc b/onglets/__pycache__/onglet1.cpython-312.pyc index 1ff1334974f64fdca5dd440f8446d4991fcbb0ae..e06d98b9ddaddfe716d8f0e3f5f19e551df02c85 100644 Binary files a/onglets/__pycache__/onglet1.cpython-312.pyc and b/onglets/__pycache__/onglet1.cpython-312.pyc differ diff --git a/onglets/__pycache__/onglet2.cpython-312.pyc b/onglets/__pycache__/onglet2.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c09264a0509b4a4a92940790572ca16f12b2b8e9 Binary files /dev/null and b/onglets/__pycache__/onglet2.cpython-312.pyc differ diff --git a/onglets/onglet1.py b/onglets/onglet1.py index 509441390e32625270b9b9a2b1df359c4489d97a..e92991aafce0c01a83a876c9dcb8affdd8371291 100644 --- a/onglets/onglet1.py +++ b/onglets/onglet1.py @@ -1,3 +1,5 @@ +# Onglet pour tracer les histogrammes en barre horizontable + import streamlit as st import pandas as pd from matplotlib.colors import to_hex @@ -32,7 +34,7 @@ def display() : # visualisation des données st.header('Visualisation des données :') - st.write(df.head(5)) # pour 5 lignes + st.dataframe(df, use_container_width=True) # bloc sur les infos du dataframe st.subheader('Information sur les données :') @@ -44,20 +46,26 @@ def display() : st.write(df.dtypes) st.subheader('Choix d\'option :') - if df.shape[0]>12 : - nb_line = st.number_input( - "Nombre de lignes à afficher", - min_value=1, - max_value=len(df), - value=5, - step=1, - format="%d" # ↠force l'affichage et le retour d'un entier - ) + nb_line = st.number_input( + "Nombre de lignes à afficher", + min_value=1, + max_value=len(df), + value=5, + step=1, + format="%d" # ↠force l'affichage et le retour d'un entier + ) # choix des colonnes à tracer columns = df.columns.tolist() - col_x = st.selectbox('Choisir la colonne des valeurs (x)', columns) + float_cols = df.select_dtypes(include=['float', 'int']).columns + + if len(float_cols) > 0: + default_index = columns.index(float_cols[0]) + col_x = st.selectbox("Choisir la colonne des valeurs (x)", columns, index=default_index) + else: + col_x = st.selectbox("Choisir la colonne des valeurs (x)", columns) + col_y = st.selectbox('Choisir la colonne des catégories (y)', columns) title_choice = st.checkbox("Voulez-vous mettre un titre au graphique ?") @@ -66,7 +74,8 @@ def display() : else: title = None - model_graph = utils_graph.plot_graph(df = df.loc[:nb_line-1, :], x = col_x, y = col_y) + # création du modèle de graphe et message si erreur pour la sélection des colonnes + model_graph = utils_graph.plot_graph(df = df.loc[:nb_line-1, :], x = col_x, y = col_y, figsize=(13, nb_line*0.7)) col_nombre, col_pourcentage = st.columns(2) @@ -77,7 +86,7 @@ def display() : else: xlabel = None - nombre_choice = st.checkbox('Mettre la valeur ?') + nombre_choice = st.checkbox('Afficher la valeur absolue de chaque barre ?') if nombre_choice : nombre = st.selectbox('Nom de la colonne des valeurs :', columns) else : @@ -90,25 +99,31 @@ def display() : else: ylabel = None - pourcentage_choice = st.checkbox('Mettre le pourcentage ?') + pourcentage_choice = st.checkbox('Afficher le pourcentage de chaque barre ?') if pourcentage_choice : pourcentage = st.selectbox('Nom de la colonne des pourcentages :', columns) else : pourcentage = None - legend_choice = st.checkbox('Voulez-vous mettre une légende de couleurs ?') + # partie encadrement + options = df[col_y] + encadrement_choice = st.checkbox('Voulez-vous mettre en avant une barre ?') + if encadrement_choice : + selection = st.multiselect("Choisissez les CFA à encadrer :", options) + else : + selection = None - #annotation = st.checkbox('Voulez-cous mettre une annotation ?') - #if annotation : - # col_nombre = st.selectbox('Colonne des valeurs absolues', columns) + # partie pour la couleur des barres et de la légende + + legend_choice = st.checkbox('Voulez-vous mettre une légende de couleurs ?') - if legend_choice: col_color = st.selectbox('Choisir la colonne des numéros de couleurs', columns) try : # Premier tracé pour générer list_colors model_graph.barh_subplot(colors=col_color, show=False, title= title, xlabel= xlabel, ylabel=ylabel) + colors_rgba = model_graph.list_colors custom_colors = {} @@ -143,6 +158,7 @@ def display() : ) model_graph.annotation(pourcentage= pourcentage, Nombre= nombre) + model_graph.encadrer(selection=selection) st.pyplot(barh) @@ -153,6 +169,8 @@ def display() : # Tracer sans légende barh = model_graph.barh_subplot(title= title, xlabel= xlabel, ylabel= ylabel, show=False) model_graph.annotation(pourcentage= pourcentage, Nombre= nombre) + model_graph.encadrer(selection=selection) + st.pyplot(barh) if 'barh' in locals(): diff --git a/onglets/onglet2.py b/onglets/onglet2.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..e1cc3ec05a78c63757d5aa377ebd855eaecf5704 100644 --- a/onglets/onglet2.py +++ b/onglets/onglet2.py @@ -0,0 +1,36 @@ +# Onglet pour tracer les histogrammes en barre horizontable + +import streamlit as st +import pandas as pd +from matplotlib.colors import to_hex +from io import BytesIO +import sys +import os + +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'utils'))) + +import utils.utils_graph as utils_graph + +def display() : + upload_file = st.file_uploader('Charger vos données en format .csv ou .xlsx.') + + if upload_file is not None : + + # chargement des données + filename = upload_file.name + + # vérifie la forme des données + if filename.endswith('.csv'): + df = pd.read_csv(upload_file) + st.success("Fichier CSV chargé avec succès.") + + elif filename.endswith('.xlsx') or filename.endswith('.xls'): + df = pd.read_excel(upload_file) + st.success("Fichier Excel chargé avec succès.") + + else: + st.error("Format de fichier non supporté. Veuillez charger un .csv ou un .xlsx.") + + # visualisation des données + st.header('Visualisation des données :') + st.dataframe(df, use_container_width=True) \ No newline at end of file diff --git a/utils/__pycache__/utils_graph.cpython-312.pyc b/utils/__pycache__/utils_graph.cpython-312.pyc index ea4c79ac17785b3e3b8ee181f5d9b7f84ff1dd50..92e5e96d7c12e3e383c07d41a10a8ccc3aa0c2d9 100644 Binary files a/utils/__pycache__/utils_graph.cpython-312.pyc and b/utils/__pycache__/utils_graph.cpython-312.pyc differ diff --git a/utils/utils_graph.py b/utils/utils_graph.py index c692e337b5d06ec8d4ea379cc390c9d280966173..6626376303746ebba98157db03d016344456b5a7 100644 --- a/utils/utils_graph.py +++ b/utils/utils_graph.py @@ -73,6 +73,8 @@ class plot_graph() : self.ax.set_ylabel(ylabel) self.ax.set_title(title, pad=30, color='black', fontsize=16) + self.ax.set_xlim(0, max(self.x)*1.15) + self.ax.grid(axis='x', linestyle='-', alpha=0.2) plt.tight_layout() @@ -112,10 +114,12 @@ class plot_graph() : return self.fig - def encadrer(self) : + def encadrer(self, selection) : - """iscod_index = self.df[self.df['denomination_cfa'] == 'ISCOD'].index.values[0] - bars[iscod_index].set_edgecolor('black') # Couleur de la bordure - bars[iscod_index].set_linewidth(2) # Épaisseur de la bordure """ + if selection : + for bar, label in zip(self.bars,self.y): + if label in selection: + bar.set_edgecolor('#FCA616') + bar.set_linewidth(3) - pass \ No newline at end of file + return self.fig \ No newline at end of file diff --git a/utils/utils_hist.py b/utils/utils_hist.py new file mode 100644 index 0000000000000000000000000000000000000000..de482eca8567a82e455f936ca7e8ee523f79d9e9 --- /dev/null +++ b/utils/utils_hist.py @@ -0,0 +1,21 @@ +import numpy as np +import matplotlib.pyplot as plt +import pandas as pd +from matplotlib.colors import LinearSegmentedColormap, ListedColormap +import matplotlib.patches as mpatches + +# partie sur les légendes à revoir + +c_map = ['#000091', '#AB0345'] +cmap = LinearSegmentedColormap.from_list("custom_cmap", c_map) + +class plot_graph() : + def __init__(self,df, x, y, figsize = (10,6), ax = None, fig = None): + self.df = df + self.x = self.df[x] + self.y = self.df[y] + self.figsize = figsize + self.ax = ax + self.fig = fig + + \ No newline at end of file