Exemples de comment tracer un diagramme en bâtons avec Matplotlib et la fonction bar du module pyplot.
Tracer un diagramme en bâtons avec Matplotlib

import matplotlib.pyplot as pltimport numpy as npfig = plt.figure()x = [1,2,3,4,5,6,7,8,9,10]height = [8,12,8,5,4,3,2,1,0,0]width = 1.0plt.bar(x, height, width, color='b' )plt.savefig('SimpleBar.png')plt.show()
Changer de style d'un diagramme en bâtons avec Matplotlib
On peut ensuite modifier le style
width = 0.05 # modifier la largeur des bâtonsplt.xlim(0,11) # Modifier les limites sur xplt.ylim(0,14) # Modifier les limites sur ypylab.xticks(x, BarName, rotation=40) # ajouter des labels aux bâtonsplt.scatter([i+width/2.0 for i in x],height,color='k',s=40) # ajouter un cercle au sommet du bâtonetc

import matplotlib.pyplot as pltimport numpy as npimport pylabfig = plt.figure()x = [1,2,3,4,5,6,7,8,9,10]height = [8,12,8,5,4,3,2,1,2,4]width = 0.05BarName = ['a','b','c','d','e','f','g','h','i','j']plt.bar(x, height, width, color=(0.65098041296005249, 0.80784314870834351, 0.89019608497619629, 1.0) )plt.scatter([i+width/2.0 for i in x],height,color='k',s=40)plt.xlim(0,11)plt.ylim(0,14)plt.grid()plt.ylabel('Counts')plt.title('Diagramme en Batons !')pylab.xticks(x, BarName, rotation=40)plt.savefig('SimpleBar.png')plt.show()
Références
| Liens | Site |
|---|---|
| pyplot | matplotlib doc |
| Matplotlib - label each bin | stackoverflow |
| Aligning rotated xticklabels with their respective xticks | stackoverflow |
| How to add an integer to each element in a list? | stackoverflow |
| matplotlib set yaxis label size | stackoverflow |
| pylab_examples example code: barchart_demo2.py | matplotlib example |
