Comment tracer un simple histogramme avec matplotlib de python ?

Published: 01 décembre 2014

DMCA.com Protection Status

Exemple de comment tracer rapidement un histogramme avec matplotlib à partir d'un ensemble de données:

Tracer un simple histogramme avec matplotlib

Pour tracer un histogramme (Note: ne pas confondre histogramme et diagramme en bâtons) avec matplotlib il existe la fonction hist() du module pyplot, exemple:

Comment tracer un simple histogramme avec matplotlib de python ?
Comment tracer un simple histogramme avec matplotlib de python ?

import matplotlib.pyplot as plt

data = [1,2,2,3,3,3,4,4,5]

plt.hist(data)

plt.title('How to plot a simple histogram in matplotlib ?', fontsize=10)

plt.savefig("plot_simple_histogramme_matplotlib_01.png")

plt.show()

Histogramme normalisé à 1

Pour normalisé l'histogramme on peut ajouter l'option "normed", exemple Histogramme normalisé à 1

Comment tracer un simple histogramme avec matplotlib de python ?
Comment tracer un simple histogramme avec matplotlib de python ?

plt.hist(data, normed=1)

plt.title('How to plot a simple histogram in matplotlib ?', fontsize=10)

plt.savefig("plot_simple_histogramme_matplotlib_02.png")

plt.show()

Définir le nombre de classes

Par défaut matplotlib calcule le nombre de classes, mais il est aussi possible d'ajouter en option le nombre de classes avec l'argument "bin", exemple avec bin = 2

Comment tracer un simple histogramme avec matplotlib de python ?
Comment tracer un simple histogramme avec matplotlib de python ?

plt.hist(data, bins = 2)

plt.title('How to plot a simple histogram in matplotlib ?', fontsize=10)

plt.savefig("plot_simple_histogramme_matplotlib_03.png")

plt.show()

Définir le nombre de classes et leurs largeurs

On peut aussi donner une liste dans l'option bin afin de définir un nombre de classes et leurs largeurs, exemple avec bin = [0,2,4,6] (ici on a trois classes d'intervalle [0,2[; [2,4[ et [4,6[)

Comment tracer un simple histogramme avec matplotlib de python ?
Comment tracer un simple histogramme avec matplotlib de python ?

plt.hist(data, bins = [0,2,4,6])

plt.title('How to plot a simple histogram in matplotlib ?', fontsize=10)

plt.savefig("plot_simple_histogramme_matplotlib_04.png")

plt.show()

Imposer le nombre de classes et leurs largeurs est important pour pouvoir comparer avec un autre histogramme:

Comment tracer un simple histogramme avec matplotlib de python ?
Comment tracer un simple histogramme avec matplotlib de python ?

    data = [1,1,2,3,3,4,4,5,5]

plt.hist(data, bins = [0,2,4,6])

plt.title('How to plot a simple histogram in matplotlib ?', fontsize=10)

plt.savefig("plot_simple_histogramme_matplotlib_04.png")

plt.show()

Retrouver les paramètres de l'histogramme

De plus, la fonction plt.hist() retourne plusieurs informations que l'on peut récupérer comme ceci:

n, bins, patches = plt.hist(data, bins=[0,2,4,6])

print(n)
print(bins)
print(patches)

donne ici

[ 2.  3.  4.]
[0 2 4 6]
<a list of 3 Patch objects>

où n contient l'amplitude de chaque classe, bins donne la valeur minimum et maximum de chaque classe et patches contient des informations sur la mise en forme de l'histogramme (voir) pour plus d'informations.

Pour les diagrammes en bâtons veuillez consulter l'article suivant.

Références

Liens Site
hist() matplotlib doc
bar matplotlib doc
histogramme wikipedia
diagramme en bâtons wikipedia
pylab_examples example code: histogram_demo.py matplotlib doc
pylab_examples example code: histogram_demo_extended.py matplotlib doc
Matplotlib - label each bin stackoverflow
Image

of