Examples of how to plot a simple bar chart using matplotlib and the function bar.
Plot a simple bar char with matplotlib
import matplotlib.pyplot as plt
import numpy as np
fig = 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.0
plt.bar(x, height, width, color='b' )
plt.savefig('SimpleBar.png')
plt.show()
Change the style of a bar char with matplotlib
The bar chart style can be then changed
width = 0.05 # modifier la largeur des bâtons
plt.xlim(0,11) # Modifier les limites sur x
plt.ylim(0,14) # Modifier les limites sur y
pylab.xticks(x, BarName, rotation=40) # ajouter des labels aux bâtons
plt.scatter([i+width/2.0 for i in x],height,color='k',s=40) # ajouter un cercle au sommet du bâton
etc
import matplotlib.pyplot as plt
import numpy as np
import pylab
fig = 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.05
BarName = ['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()
References
Links | 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 |