1

I'm trying to make a histogram of some data that is being stored in an ndarray. The histogram is part of a set of analysis which I've made into a class in a python program. The part of the code that isn't working is below.

def histogram(self, iters):
    samples = T.MCMC(iters) #Returns an [iters,3,4] ndarray
    histAC = plt.figure(self.ip) #plt is matplotlib's pyplot
    self.ip+=1 #defined at the beginning of the class to start at 0
    for l in range(0,4):
        h = histAC.add_subplot(2,(iters+1)/2,l+1)
        for i in range(0,0.5*self.chan_num):
            intAvg = mean(samples[:,i,l])
            print intAvg
            for k in range(0,iters):
                samples[k,i,l]=samples[k,i,l]-intAvg
            print "Samples is ",samples
            h.hist(samples,bins=5000,range=[-6e-9,6e-9],histtype='step')     
            h.legend(loc='upper right')
            h.set_title("AC Pulse Integral Histograms: "+str(l))
    figname = 'ACHistograms.png'
    figpath = 'plot'+str(self.ip)
    print "Finished!"
    #plt.savefig(figpath + figname, format = 'png')

This gives me the following error message:

File "johnmcmc.py", line 257, in histogram
  h.hist(samples,bins=5000,range=[-6e-9,6e-9],histtype='step') #removed label=apdlabel      
File "/x/tsfit/local/lib/python2.6/site-packages/matplotlib/axes.py", line 7238, in hist
  ymin = np.amin(m[m!=0]) # filter out the 0 height bins
File "/x/tsfit/local/lib/python2.6/site-packages/numpy/core/fromnumeric.py", line 1829, in amin
  return amin(axis, out)
ValueError: zero-size array to ufunc.reduce without identity

The only search results I've found have been multiple copies of the same two conversations, from which the only thing I learned was that python histograms don't like getting fed empty arrays, which is why I added the print statement right above the line that's giving me trouble to make sure the array isn't empty.

Has anyone else come across this error before?

3
  • do numpy histograms handle 3D data OK? What happens if you pass np.ravel(samples) or samples.flat() to h.hist?
    – mgilson
    Jul 19, 2012 at 19:47
  • 2
    That's the error you'd get if none of the values fell into the range [-6e-9, 6e-9], but since I don't know the units I don't know if those are unusually small values or not. Are you sure they all do? (I'm also not sure I understand what you're doing with samples, but that's another question.)
    – DSM
    Jul 19, 2012 at 19:59
  • @DSM , that solves it. Tinkered with the range a bit and it worked. Thanks a lot! Jul 19, 2012 at 20:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.