python numpy histogram

Machiel Kolstein mkolstein at ifae.es
Tue Jun 18 07:23:32 EDT 2019


Hi, 

I get the following error: 

ERROR: 
Traceback (most recent call last):
  File "exponential_distr.py", line 32, in <module>
    numpy.histogram(data_array, bins=100, range=20000)
  File "/usr/lib/python2.7/dist-packages/numpy/lib/function_base.py", line 499, in histogram
    mn, mx = [mi + 0.0 for mi in range]
TypeError: 'int' object is not iterable

with the code shown below. 
I guess I am using "numpy.histogram" below. Also, is there a way to fill the histogram on an event by event base without using the clumsy way of constructing an array as I do below?
And - final question - how can I most easily plot the histogram without having to define - again - the bins etc...?

CODE: 

import numpy
import numpy.random

rate = float(1e5)
average_dt = 1.0/rate
print "average dt: ", average_dt

calc_average = 0.0
total_num=1000.0
isFirst = True
for index in range(0, int(total_num)): 
    time = numpy.random.exponential(average_dt)
    #    print "time: ", time, " = ", time*1e9, " ns" 
    calc_average = calc_average + time
    
    # Idiot python way of constructing an array (God, I hate python...)
    if (isFirst == True): 
        data_array = time
        isFirst = False
    else:    
        data_array = numpy.hstack((data_array, time))

calc_average = calc_average/total_num
print "calculated average: ", calc_average, " = ", calc_average*1e9, " ns"
print "data_array: ", data_array

numpy.histogram(data_array, bins=100, range=20000)
#	import matplotlib.pyplot as plt
#	plt.hist(data_array, bins=100, range=20000)
#	plt.show()




-- 
Avís -
Aviso - Legal Notice - (LOPD) - http://legal.ifae.es 
<http://legal.ifae.es/>



More information about the Python-list mailing list