comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: PSYM=10 problem
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: PSYM=10 problem [message #17827] Tue, 16 November 1999 00:00
Liam Gumley is currently offline  Liam Gumley
Messages: 473
Registered: November 1994
Senior Member
Laurent Chardon wrote:
> Is there a good reason why the first bin plotted by the plot/PSYM=10
> combination is half the size of all the others? Can I get around this
> behaviour? I want all the bins to be of equal size.

A few months ago in this newsgroup, David Fanning convinced me that
PSYM=10 would never give an accurate representation of a histogram, and
that the only way to do it right is to plot the histogram yourself. As
you've noted, the problem is getting the edges of the bins in the right
position. I came up with the following procedure which I believe
computes and plots a 'correct' histogram (let me know if I'm wrong!):

;---cut here---
PRO HIST_PLOT, DATA, MIN=MIN_VALUE, MAX=MAX_VALUE, $
BINSIZE=BINSIZE, NORMALIZE=NORMALIZE, FILL=FILL, $
_EXTRA=EXTRA_KEYWORDS

; DATA Array of data
; MIN Minimum data value for computing histogram
; (default is MIN(DATA))
; MAX Maximum data value for computing histogram
; (default is MAX(DATA))
; BINSIZE Binsize (default is to create 100 bins)
; NORMALIZE If set, normalize the histogram
; FILL If set, fill the histogram
; Also accepts any keywords recognized by PLOT or POLYFILL

;- Check arguments
if n_params() ne 1 then message, 'Usage: HIST_PLOT, DATA'
if n_elements(data) eq 0 then message, 'DATA is undefined'

;- Check keywords
if n_elements(min_value) eq 0 then min_value = min(data)
if n_elements(max_value) eq 0 then max_value = max(data)
if n_elements(binsize) eq 0 then $
binsize = (max_value - min_value) * 0.01
binsize = binsize > ((max_value - min_value) * 1.0e-5)

;- Compute histogram
hist = histogram(float(data), binsize=binsize, $
min=min_value, max=max_value)
hist = [hist, 0L]
nhist = n_elements(hist)

;- Normalize histogram if required
if keyword_set(normalize) then $
hist = hist / float(max(hist))

;- Compute bin values
bins = lindgen(nhist) * binsize + min_value

;- Create plot arrays
x = fltarr(2 * nhist)
x[2 * lindgen(nhist)] = bins
x[2 * lindgen(nhist) + 1] = bins
y = fltarr(2 * nhist)
y[2 * lindgen(nhist)] = hist
y[2 * lindgen(nhist) + 1] = hist
y = shift(y, 1)

;- Plot the histogram
plot, x, y, _extra=extra_keywords

;- Fill the histogram if required
if keyword_set(fill) then $
polyfill, [x, x[0]], [y, y[0]], _extra=extra_keywords

END
;---cut here---

As verification, I submit the following example:

data = dist(256)
hist_plot, data
hist_plot, data, binsize=1.0, xrange=[-1, 10], yrange=[0, 20]
result = where(data ge 0.0 and data lt 1.0, count)
print, count
1
result = where(data ge 1.0 and data lt 2.0, count)
print, count
8
result = where(data ge 2.0 and data lt 3.0, count)
print, count
16

Cheers,
Liam.

--
Liam E. Gumley
Space Science and Engineering Center, UW-Madison
http://cimss.ssec.wisc.edu/~gumley
Re: PSYM=10 problem [message #17829 is a reply to message #17827] Tue, 16 November 1999 00:00 Go to previous message
Steve Cox is currently offline  Steve Cox
Messages: 3
Registered: November 1999
Junior Member
Laurent Chardon wrote:
>
> Is there a good reason why the first bin plotted by the plot/PSYM=10
> combination is half the size of all the others? Can I get around this
> behaviour? I want all the bins to be of equal size.

I've had this problem too, and worked around it by duplicating
the first and last elements of the data, and by adding the end
points to the bin centers.

If data is your histogram data, and binBounds is an array
of the histogram bin boundaries:

sizeData=N_Elements(data)
plotData=[data(0),data,data(sizeData-1)] ;duplicate first and last

sizeBinBounds=N_Elements(binBounds)
binCenters(i)=FltArr(sizebinBounds-1)
FOR i=0,sizeBinBounds-2 DO binCenters(i)=binBounds(i)+binBounds(i+1))/2.

;add highest and lowest bin boundaries to the bin center array
binLabels=[binBounds(0),binCenters,binBounds(sizeBinBounds-1 )]

Plot,binLabels,plotData,PSym=10

I'll trust the maxim that the best way to find out that one's method
is needlessly complicated is to post it to comp.lang.idl-pvwave.

-Steve Cox
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Command input window in Windows: why only one line?
Next Topic: Re: Command input window in Windows: why only one line? Why, generally does the windows handling not work?

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:13:51 PDT 2025

Total time taken to generate the page: 0.00635 seconds