Chad Bender (cbender@mail.astro.sunysb.edu) writes:
> I'm encountering some confusion using the histogram function when
> specifying the min, max, and binsize keywords.
Oh, right. I'm glad I'm not the only one this drives
crazy.
I spend several chapters in a book talking about this oddity
in Histogram, but I have never gotten around to writing a
wrapper for the command. (Which seems really odd to me.)
In any case, he is the relevant code from the program in
the book:
; Calculate the histogram.
histdata = Histogram(image, Binsize=binsize, Max=Max(image), $
Min=Min(image))
; Have to fudge the bins and histdata variables to get the
; histogram plot to make sense.
npts = N_Elements(histdata)
halfbinsize = binsize / 2.0
bins = Findgen(N_Elements(histdata)) * binsize + Min(image)
binsToPlot = [bins[0], bins + halfbinsize, bins[npts-1] + binsize]
histdataToPlot = [histdata[0], histdata, histdata[npts-1]]
xrange = [Min(binsToPlot), Max(binsToPlot)]
; Plot the histogram of the display image. Axes first.
Plot, binsToPlot, histdataToPlot, $ ; Fudged histogram and bin data.
Background=backcolor, $ ; Background color of the display.
Charsize=thisCharsize, $ ; Character size.
Color=axiscolor, $ ; The color of the axes.
Max_Value=max_value, $ ; The maximum value of the plot.
NoData=1, $ ; Draw the axes only. No data.
Position=histoPos, $ ; Position of the plot.
Title='Image Histogram', $ ; The title of the plot.
XRange=xrange, $ ; The X data range.
XStyle=1, $ ; Exact axis scaling.
XTickformat='(I6)', $ ; Format of the X axis annotations.
XTitle='Image Value', $ ; The title of the X axis.
YMinor=1, $ ; One minor tick mark on X axis.
YRange=[0,max_value], $ ; The Y data range.
YStyle=1, $ ; Exact axis scaling.
YTickformat='(I6)', $ ; Format of the Y axis annotations.
YTitle='Pixel Density', $ ; The title of the Y axis.
_Extra=extra ; Pass any extra PLOT keywords.
; Overplot the histogram data in the data color.
OPlot, binsToPlot, histdataToPlot, PSym=10, Color=dataColor
; Make histogram boxes by drawing lines in data color.
FOR j=1L,N_Elements(bins)-2 DO BEGIN
PlotS, [bins[j], bins[j]], [!Y.CRange[0], histdata[j] < max_value], $
Color=dataColor
ENDFOR
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting, Inc.
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|