Pour tracer un histogramme à partir d'un ensemble de données il existe la fonction matplotlib hist(). Pour tracer deux histogrammes on peut donc tout simplement faire appelle deux fois a cette fonction, exemple:

import numpy as npimport matplotlib.pyplot as pltdata1 = np.random.randn(100000)plt.hist(data1, bins=25, normed=1,color="lightblue")mu = 5.0sigma = 3.0data2 = np.random.randn(100000) * sigma + muplt.hist(data2, bins=50, normed=1,color="lightcoral")plt.title('Two histograms')plt.grid()plt.savefig("two_histograms_01.png", bbox_inches='tight')plt.show()
Pour améliorer on peut rendre le second histogramme transparent avec l'option alpha:

import numpy as npimport matplotlib.pyplot as pltdata1 = np.random.randn(100000)plt.hist(data1, bins=25, normed=1,color="lightblue")mu = 5.0sigma = 3.0data2 = np.random.randn(100000) * sigma + muplt.hist(data2, bins=50, normed=1,color="red", alpha=0.5)plt.title('Two histograms (alpha=0.5)')plt.grid()plt.savefig("two_histograms_02.png", bbox_inches='tight')plt.show()
et aussi définir un intervalle fixe avec bins = np.linspace(-5, 15, 100):

import numpy as npimport matplotlib.pyplot as pltbins = np.linspace(-5, 15, 100)data1 = np.random.randn(100000)plt.hist(data1, bins=bins, normed=1,color="lightblue")mu = 5.0sigma = 3.0data2 = np.random.randn(100000) * sigma + muplt.hist(data2, bins=bins, normed=1,color="red", alpha=0.5)plt.title('Two histograms (alpha=0.5 + same bins size)')plt.grid()plt.savefig("two_histograms_03.png", bbox_inches='tight')plt.show()
Références
| Liens | Site |
|---|---|
| hist() | matplotlib doc |
| Plot two histograms at the same time with matplotlib | stackoverflow |
| pylab_examples example code: histogram_demo.py | matplotlib doc |
