4

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

twi
  • 127
  • 1
  • 3
  • 8

1 Answers1

3

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)
zck
  • 311
  • 1
  • 3
  • `range` is a default Python function. Using it as a variable is going to cause problems, so I'd strongly suggest calling it something else. – m13op22 Oct 24 '19 at 16:01
  • Thanks for the edit and for pointing this out. This was so long ago that it seems like a silly mistake even when trying to explain something. It seems I was also trying to use the same variables – zck Oct 25 '19 at 17:15