Mis a jour le 2024-03-17, 13:2

Histogramme

Histogramme :
Exemple simple : x = [1, 2, 2, 3, 4, 4, 4, 4, 4, 5, 5] pyplot.hist(x, range = (0, 5), bins = 5, color = 'yellow', edgecolor = 'red') pyplot.xlabel('valeurs') pyplot.ylabel('nombres') pyplot.title('Exemple d\' histogramme simple')
Attention : si la liste de valeurs données est particulière une erreur se produit :
Paramètres des histogrammes : x = [1, 2, 2, 3, 4, 4, 4, 4, 4, 5, 5] pyplot.hist(x, range = (0, 5), bins = 5, color = 'yellow', edgecolor = 'red', density = True, hatch = 'x', orientation = 'horizontal', rwidth = 0.8, log = True) pyplot.ylabel('valeurs') pyplot.xlabel('frequences') pyplot.title('Histogramme horizontal')
Affichage de l'histogramme de 2 séries (les 2 couleurs sont obligatoires, par contre la couleur des bordures et les rayures ne sont communs) : x1 = [1, 2, 2, 3, 4, 4, 4, 4, 4, 5, 5] x2 = [1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 5, 5] bins = [x + 0.5 for x in range(0, 6)] pyplot.hist([x1, x2], bins = bins, color = ['yellow', 'green'], edgecolor = 'red', hatch = '/', label = ['x1', 'x2'], histtype = 'bar') # bar est le defaut pyplot.ylabel('valeurs') pyplot.xlabel('nombres') pyplot.title('2 series') pyplot.legend()
Affichage de l'histogramme de 2 séries, mais sous forme superposées, l'un au dessus de l'autre. x1 = [1, 2, 2, 3, 4, 4, 4, 4, 4, 5, 5] x2 = [1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 5, 5] bins = [x + 0.5 for x in range(0, 6)] pyplot.hist([x1, x2], bins = bins, color = ['yellow', 'green'], edgecolor = 'red', hatch = '/', label = ['x1', 'x2'], histtype = 'barstacked') pyplot.ylabel('valeurs') pyplot.xlabel('nombres') pyplot.title('2 series superposees') pyplot.legend()
on peut afficher 2 histogrammes superposés, l'un sur l'autre, le 2ème ayant un certain niveau de transparence pour ne pas complètement cacher l'autre : x1 = [1, 2, 2, 3, 4, 4, 4, 4, 4, 5, 5] x2 = [1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 5, 5] bins = [x + 0.5 for x in range(0, 6)] pyplot.hist(x1, bins = bins, color = 'yellow', edgecolor = 'red', hatch = '/', label = 'x1') pyplot.hist(x2, bins = bins, color = 'green', alpha = 0.5, edgecolor = 'blue', hatch = '\', label = 'x2') pyplot.ylabel('valeurs') pyplot.xlabel('nombres') pyplot.title('superpose') pyplot.legend()
Affichage en parallèle d'un histogramme et d'un histogramme cumulé : x = [1, 2, 2, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5] pyplot.hist(x, bins = 5, color = 'skyblue') pyplot.xlabel('valeurs') pyplot.ylabel('freq') ax2 = pyplot.gca().twinx() ax2.hist(x, bins = 5, cumulative = True, histtype = 'step', density = True, color = 'blue') ax2.set_ylabel('pourcentage cumulé') pyplot.tight_layout()

Copyright python-simple.com
programmer en python, tutoriel python, graphes en python, Aymeric Duclert