If I use histogram of matplotlib , I can choose the number of bins. But how can I choose the number of bins at histogram of numpy?
import matplotlib.pyplot as plt
import numpy as np
array = [1,3,4,4,8,9,10,12]
range = int((max(array)) - min(array))+1
x, bins, patch = plt.hist(array, bins=range)
In this case range = number of bins = (12-1)+1 = 12
So the result is x = [ 1. 0. 1. 2. 0. 0. 0. 1. 1. 1. 0. 1.]
But the result of numpy is
hist, bin_edges = np.histogram(array, density=False)
numpy = [1 1 2 0 0 0 1 1 1 1] numpy_bin = [ 1. 2.1 3.2 4.3 5.4 6.5 7.6 8.7 9.8 10.9 12. ]
When using numpy , how can I choose the number of bins(= int((max(array)) - min(array))+1)
I want the same result like matplotlib
Matplotlib is using numpys histogram, to pass number of bins simply add bins=bin_range
as keyword argument to np.histogram
:
hist, edges = np.histogram(array, bins=bin_range, density=False)
If bin_range
is an integer number you get bin_range
amount of equal sized bins. The default value for bins in np.histogram
is bins='auto'
which uses an algorithm to decide the number of bins. Read more at: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.histogram.html
array = [1,3,4,4,8,9,10,12]
bin_range = int((max(array)) - min(array))+1
x, bins, patch = plt.hist(array, bins=bin_range)
x
array([ 1., 0., 1., 2., 0., 0., 0., 1., 1., 1., 0., 1.])
hist, edges = np.histogram(array, bins=bin_range)
hist
array([1, 0, 1, 2, 0, 0, 0, 1, 1, 1, 0, 1], dtype=int64)
bins == edges
array([ True, True, True, True, True, True, True, True, True,
True, True, True, True], dtype=bool)