Matplotlib Tutorial Part 03 - Bar Charts and Histograms


In [1]:
%matplotlib inline
import matplotlib.pyplot as plt

Bar Chart


In [14]:
X = [1,5,8,1,4,6,8]
Y = [1,8,2,4,8,9,3]

plt.bar(X,Y, label="Bar1")

plt.xlabel('x')
plt.ylabel('y')
plt.title('Bar Chart')
plt.legend()

plt.show()


Multiple Bars Chart


In [11]:
X = [1,0,8,1,4,6,8]
Y = [1,8,2,4,8,9,3]

X2 = [1,3,5,9,11]
Y2 = [8,7,1,6,1]

plt.bar(X,Y, label="Bar1")
plt.bar(X2,Y2, label="Bar2")

plt.xlabel('x')
plt.ylabel('y')
plt.title('Bar Chart')
plt.legend()

plt.show()


Adding different Colours


In [23]:
X = [1,0,8,3,4]
Y = [1,8,2,4,8]

X2 = [1,0,8,3,4]
Y2 = [9,7,1,6,1]

plt.bar(X,Y, label="Bar1", color='r')
plt.bar(X2,Y2, label="Bar2", color='c')

plt.xlabel('x')
plt.ylabel('y')
plt.title('Bar Chart')
plt.legend()

plt.show()


Swapping order


In [24]:
X = [1,0,8,3,4]
Y = [1,8,2,4,8]

X2 = [1,0,8,3,4]
Y2 = [9,7,1,6,1]

plt.bar(X2,Y2, label="Bar2", color='c')
plt.bar(X,Y, label="Bar1", color='r')

plt.xlabel('x')
plt.ylabel('y')
plt.title('Bar Chart')
plt.legend()

plt.show()


Histogram vs Bar Charts


In [30]:
from random import randint

X = [randint(0,110) for i in range(100)]
Y = [x for x in range(len(X))]

plt.bar(X,Y, label="Bar1", color='r')

plt.xlabel('x')
plt.ylabel('y')
plt.title('Bar Chart')
plt.legend()

plt.show()

bins = [i for i in range(0,max(X),10)]
plt.hist(X,bins,histtype='bar',rwidth = 0.8)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Histogram')
plt.show()