Thursday, June 11, 2020

histogram using matplotlib

Introduction to matplotlib 

Matplotlib is a plotting library for the Python and its numerical mathematics extension NumPy. Matplotlib is a large and detailed library for static, animated, and interactive visualizations with Python. It can also be used with graphics toolkits like PyQt and wxPython.

The matplotlib Object Hierarchy


We need to understand object hierarchy before diving into matplotlib concepts. The “hierarchy” can be understood as a tree-like structure of matplotlib objects beneath each plot.

The figure object is the container for a matplotlib graphic. The figure object holds multiple Axes objects. Axes do not man here a plural of axis, rather it represents an individual graph or plot.

matplotlib numpy


Plotting with matplotlib


We can easily plot various types of graphs, visualizations, and animations with the help of matplotlib, for example,

import numpy as np 
from matplotlib import pyplot as plot 

x = np.arange(1,11) 
y = x * x + 5 
plot.title("EXample plot") 
plot.xlabel("x axis label") 
plot.ylabel("y axis label") 
plot.plot(x,y) 
plot.show()
The result will appear as,

matplotlib



Plotting the Histogram


The following methods are available in Numpy for the histogram routines,

  • histogram(a[, bins, range, normed, weights, …])
  • histogram2d(x, y[, bins, range, normed, …])
  • histogramdd(sample[, bins, range, normed, …])

The rectangles of the equal horizontal size corresponding to class interval are known as bins and variable height corresponding to the frequency.

for example,

import numpy as np
#with bins
print(np.histogram(np.arange(4), bins=np.arange(5), density=True))
#with weights
print('\n')
print(np.histogram([[1, 2, 1], [1, 0, 1]], bins=[0,1,2,3]))

#Output
(array([0.25, 0.25, 0.25, 0.25]), array([0, 1, 2, 3, 4]))


(array([1, 4, 1]), array([0, 1, 2, 3]))

This is the mathematical representation of histogram data. We can convert this numeric representation into a visual representation with matplotlib.

For example,

from matplotlib import pyplot as plt 
import numpy as np  
   
plt.hist(np.histogram(np.array([14,2,7,9,12,5,5,8,7,11]), bins=np.arange(6), density=True))
plt.title("histogram") 
plt.show()

The result appears as,


matplotlib histogram


We can plot a simple histogram as,

from matplotlib import pyplot as plt 
import numpy as np  
   
plt.hist(np.array([1,2,2,3,3,3,4,4,4,4,5,5,6]),
bins=[0,1,2,3,4,5,6,7])
plt.title("simple histogram") 
plt.show()

The result appears as,

matplotlib histogram