63673

How to choose number of bins in numpy.histogram?

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

Answer1:

Matplotlib is using numpys histogram, to pass number of bins simply add bins=range as keyword argument to np.histogram:

hist, edges = np.histogram(array, bins=range, density=False)

If range is an integer number you get range ammount 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] range = int((max(array)) - min(array))+1 x, bins, patch = plt.hist(array, bins=range) x array([ 1., 0., 1., 2., 0., 0., 0., 1., 1., 1., 0., 1.]) hist, edges = np.histogram(array, bins=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)

Recommend

  • Error in bind_rows_(x, .id) : Column can't be converted from factor to numeric
  • Use Hourly data in ts and window function
  • Plotting in rworldmaps
  • Missing config.ru when using rackup [closed]
  • How to make sure the appium node is connected to only one phone?
  • Import object class definitions to Active Directory (AD LDS)
  • how to set android splash screen using config.xml of cordova2.6
  • unary minus for 0x80000000 (signed and unsigned)
  • (node:71307) [DEP0079] DeprecationWarning
  • How do I get an image w/ the exact size in pixels, when using bbox_inches='tight' in matpl
  • Moon orbit in relation to sun
  • How to get scaled bitmap with respect to screen size with out getting Out of memory exception
  • Imagemagick set interline spacing?
  • Aspect advising other aspects
  • Can't get LogCat (Alcatel OneTouch Evolve)
  • Defining variable by logical subseting on time interval in data.table
  • What's a fast (non-loop) way to apply a dict to a ndarray (meaning use elements as keys and rep
  • Struct pointer casts
  • MongoDB GeoJSON “Can't extract geo keys from object, malformed geometry?” when inserting type P
  • Partial specialization of a class template in derived class affects base class
  • Find 5 consecutive numbers in numpy array by row, ignore duplicates
  • Put value at centre of bins for histogram
  • Is it possible to define rest argument in OCaml?
  • Make new pandas columns based on pipe-delimited column with possible repeats
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • htaccess add www if not subdomain, if subdomain remove www
  • java inputstream
  • JSON response opens as a file, but I can't access it with JavaScript
  • Android screen density dpi vs ppi
  • Accessing IRQ description array within a module and displaying action names
  • How to make Safari send if-modified-since header?
  • How to pass list parameters for each object using Spring MVC?
  • Numpy divide by zero. Why?
  • php design question - will a Helper help here?
  • AngularJs get employee from factory
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • JaxB to read class hierarchy
  • Converting MP3 duration time