Tracer un histogramme

Réflexion statistique en Python (Partie 1)

Justin Bois

Teaching Professor at the California Institute of Technology

Résultats des élections 2008 dans les swing states américains

Données obtenues de Data.gov (https://www.data.gov/)

ch1-2.003.png

Réflexion statistique en Python (Partie 1)

Générer un histogramme

import matplotlib.pyplot as plt
_ = plt.hist(df_swing['dem_share'])
_ = plt.xlabel('percent of vote for Obama')
_ = plt.ylabel('number of counties')
plt.show()
Réflexion statistique en Python (Partie 1)

Toujours étiqueter vos axes

Réflexion statistique en Python (Partie 1)

Résultats des élections 2008 dans les swing states américains

Données obtenues de Data.gov (https://www.data.gov/)

ch1-2.013.png

Réflexion statistique en Python (Partie 1)

Histogrammes avec différents regroupements (bins)

Données obtenues de Data.gov (https://www.data.gov/)

ch1-2.015.png

Réflexion statistique en Python (Partie 1)

Définir les bins d'un histogramme

bin_edges = [0, 10, 20, 30, 40, 50,
                60, 70, 80, 90, 100]
_ = plt.hist(df_swing['dem_share'], bins=bin_edges)
plt.show()

ch1-2.022.png

Réflexion statistique en Python (Partie 1)

Définir les bins d'un histogramme

_ = plt.hist(df_swing['dem_share'], bins=20)
plt.show()

ch1-2.027.png

Réflexion statistique en Python (Partie 1)

Seaborn

  • Excellent module de visualisation statistique fondé sur Matplotlib, créé par Michael Waskom
Réflexion statistique en Python (Partie 1)

Appliquer le style Seaborn

import seaborn as sns
sns.set()
_ = plt.hist(df_swing['dem_share'])
_ = plt.xlabel('percent of vote for Obama')
_ = plt.ylabel('number of counties')
plt.show()
Réflexion statistique en Python (Partie 1)

Histogramme avec style Seaborn

ch1-2.038.png

1 Données obtenues de Data.gov (https://www.data.gov/)
Réflexion statistique en Python (Partie 1)

Passons à la pratique !

Réflexion statistique en Python (Partie 1)

Preparing Video For Download...