Formation 90 min de lecture Visualisation de donnΓ©es

πŸ“¦ Visualisation avec Matplotlib

Python & Data Science Chapitre : Visualisation de donnΓ©es Sous-chapitre : 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()\n

Exemples 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()\n

Bonnes 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