Search code examples
pythonhistogrambinning

Binning for a histogram in python


I have a text files with two columns something like this

0      0.0 \
1      2.4800279266728324e-06\
2      9.823187699026918e-06\
3      2.1883453405001393e-05\
4      3.8513895910245685e-05\
5      5.9566616657733786e-05\
6      8.489267671382621e-05\
7      0.0001143420206179056\
8      0.0001477633945415465\
9      0.00018500425820479948\
10     0.00022591068993023063\
11     0.00027032728413800794\
12     0.000318097040494579\ 
13     0.00036906124381725773\
14     0.00042305933368967896\

I want to do a construct a histogram where each bins hava a width of 100 keV. How to do this coding in Python.

I tried to do the binning by taking the maximum and minimum value and divide them by the number of bins, but I think I am doing it wrong. Any help will be appreciated.


Solution

  • If you have a bin width (100 in your case) in advance, then you can do

    assigned_bin = math.floor((max - min) / bin_width)
    

    to choose which bin each observation goes in.