Formation
90 min de lecture
Visualisation de donnΓ©es
π¦ Visualisation avec Matplotlib
Objectifs d'apprentissage
π― Objectifs :\n
1CrΓ©er des graphiques linΓ©aires\n2. CrΓ©er des graphiques Γ barres\n3. CrΓ©er des histogrammes\n4. Personnaliser les graphiques
Introduction
π Matplotlib est la bibliothΓ¨que de visualisation la plus utilisΓ©e en Python.
Contenu thΓ©orique
Matplotlib :\n
PYTHON
\nimport matplotlib.pyplot as plt\n\n# Graphique linΓ©aire\nplt.plot(x, y)\n\n# Graphique Γ barres\nplt.bar(x, y)\n\n# Histogramme\nplt.hist(data)\n\n# Personnalisation\nplt.title("Titre")\nplt.xlabel("Axe X")\nplt.ylabel("Axe Y")\nplt.legend()\nplt.grid()\n\n# Affichage\nplt.show()\nExemples pratiques
π» Exemple : Graphique des ventes\n
PYTHON
\nimport matplotlib.pyplot as plt\n\nmois = ["Jan", "Fev", "Mar", "Avr", "Mai", "Juin"]\nventes = [1000, 1200, 1500, 1300, 1600, 1800]\nobjectif = [1100] * 6\n\nplt.figure(figsize=(10, 6))\nplt.plot(mois, ventes, marker="o", label="Ventes", linewidth=2)\nplt.plot(mois, objectif, "--", label="Objectif", linewidth=2)\nplt.fill_between(mois, ventes, objectif, where=(ventes >= objectif), \n color="green", alpha=0.3, label="Objectif atteint")\nplt.fill_between(mois, ventes, objectif, where=(ventes < objectif), \n color="red", alpha=0.3, label="Objectif non atteint")\n\nplt.title("Γvolution des ventes 2024", fontsize=14)\nplt.xlabel("Mois", fontsize=12)\nplt.ylabel("Ventes (β¬)", fontsize=12)\nplt.legend()\nplt.grid(True, alpha=0.3)\nplt.xticks(rotation=45)\nplt.tight_layout()\nplt.show()\nBonnes pratiques
1Toujours importer plt\nβ
2. Utiliser figure() pour taille\nβ
3. Ajouter titres et labels\nβ
4. Utiliser grid() pour lisibilitΓ©\nβ
5. tight_layout() pour Γ©viter chevauchement
Pièges à éviter
Oublier plt.show()\n
Toujours appeler plt.show()
RΓ©sumΓ©
plot() : graphique linΓ©aire\nβ
bar() : graphique Γ barres\nβ
hist() : histogramme\nβ
title(), xlabel(), ylabel() : labels\nβ
legend() : lΓ©gende\nβ
grid() : grille\nβ
show() : affichage
Ressources supplΓ©mentaires
π matplotlib.org/stable/tutorials/index.html