A simple bar chart
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
data =[5.,25.,50.,20.]
x = range(len(data)) # creates a range of 0 to 4
plt.bar(x, data)
plt.show()
A simple bar chart:
Multiple series
import matplotlib.pyplot as plt
import numpy as np
data =[[5.,25.,50.,20],
[4.,23.,51.,17],
[6., 22.,52.,19]]
X=np.arange(4)
plt.bar(X+0.00,data[0],color='b',width=0.25) # colour is blue
plt.bar(X+0.25,data[1],color='g',width=0.25) # colour is green
plt.bar(X+0.50,data[2],color='r',width=0.25) # colour is red
plt.show()
A multiple series bar chart:
For larger data sets we could automate the creation of the bars:
import matplotlib.pyplot as plt
import numpy as np
data =[[5.,25.,50.,20],
[4.,23.,51.,17],
[6., 22.,52.,19]]
color_list=['b','g','r']
gap=0.8/len(data)
for i, row in enumerate(data): # creates int i and list row
X=np.arange(len(row))
plt.bar(X+i*gap,row,width=gap,color=color_list[i])
plt.show()
Stacked bar chart
a=[5.,30.,45.,22.]
b=[5.,25.,50.,20.]
x=range(4)
plt.bar(x,a,color='b')
plt.bar(x,b,color='r',bottom=a) # bottom specifies the starting position for a bar
plt.show()
A stacked bar chart:
Back to back bar chart
women_pop=np.array([50,30,45,22])
men_pop=np.array([5,25,50,20])
x=np.arange(4)
plt.barh(x,women_pop,color='r')
plt.barh(x,-men_pop,color='b') # the minus sign creates an opposite bar
plt.show()
A back to back bar chart:
One thought on “38. Matplotlib: Bar charts”