pro my_histogram, bins, x, h, xx, mi=mi, ma=ma, plot=plot, norm=norm ; creates/plots histogram from input data x; ; input data sorted into equidistant bins with xx_i-bins/2 le x lt xx_i+bins/2 ; input: ; bins binsize ; x data array ; output: ; h histogram ; xx midpoint locations ; optional keywords ; mi start of first bin ; ma end of last bin ; set /plot to plot the histogram ; set /norm to obtain a normalized distribution (Summ(h_i)*binsize = 1.) ; keywords can be either set with keyword=value, ; or /keyword, to indicate that it has the value 1 (corresponding to'true'). ; If not set, its default value is 0 (corresponding to 'false') nx=n_elements(x) ; to avoid that mi=0 is interpreted as non-set if n_elements(mi) eq 0 then mi=min(x) if n_elements(ma) eq 1 then begin h=histogram(x,bins=bins,min=mi,max=ma,location=xx) endif else begin ma=max(x) h=histogram(x,bins=bins,min=mi,location=xx) endelse if keyword_set(norm) then h=h/float(nx*bins) xx=xx+0.5*bins if not keyword_set(plot) then return nel=n_elements(xx)-1 plot,xx,h,psym=10,xrange=[mi,xx(nel)+0.5*bins] ;psym=10 plots in histogram mode. Correct results (except for end effects) ;are obtained only if the x-data are centered! oplot,[mi,xx(0)],[h(0),h(0)] ;correct for boundary effects oplot,[xx(nel),xx(nel)+0.5*bins],[h(nel),h(nel)] ;correct for boundary effects return end