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

Home » Public Forums » archive » Re: Question on plotting a histogram with original values
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: Question on plotting a histogram with original values [message #8129] Thu, 06 February 1997 00:00
thompson is currently offline  thompson
Messages: 584
Registered: August 1991
Senior Member
David van Kuijk <kuijk@mpi.nl> writes:

> Hi

> I am using IDL3.0

> When I use the histogram-function I find it very annoying that I can't see
> which range of data has been put in which bin. Has anybody written a
> function which returns these values, or the mean values for each bin?

> To give an example:
> PRO testHisto

> testAr = [-44,0,-42,-58,-48,-25,-26,-1,-46,-36,-16,-55,-6,-81,10,-31, -38,$
> -34,-49,-80,2,-40,-65,-3,10,-31,-85,-34,-27,-14,-41,-73,-67, 3,-51,$
> -6,0,-107,-7,-36,-33,-59,-2,-99,-22]
> testHisto = histogram(testAr, BINSIZE = 10)
> plot, testHisto, PSYM = 10
> END

> This program will plot a nice histogram, but the values on the x-axis just
> show the rank number of the bins, and the information on the ranges of the
> original data is lost.

> I am sure the problem is solvable, but I don't want to spend too much time on
> this if somebody else already has found a solution.

You might try these routines. (I've commented out a reference to an old system
variable (!BCOLOR) that you won't have.)

Bill Thompson

============================================================ ===================
PRO PLOT_HISTO,ARRAY,STEPS,HISTO,TITLE=TITLE,XTITLE=XTITLE, $
YTITLE=YTITLE,XTYPE=XTYPE,YTYPE=YTYPE,DELTA=DELTA, $
BCOLOR=BCOLOR,XRANGE=XRANGE,YRANGE=YRANGE
;+
; Project : SOHO - CDS
;
; Name : PLOT_HISTO
;
; Purpose : Plots a histogram from the variable ARRAY.
;
; Category : Class3, Graphics
;
; Explanation : Plots a histogram from the variable ARRAY. Calls FORM_HISTO to
; decide what the coarseness of the histogram should be. Then
; PLOTBAR is called to plot the histogram.
;
; Syntax : PLOT_HISTO, ARRAY [, STEPS, HISTO ]
;
; Examples :
;
; Inputs : ARRAY = Array to plot histogram of.
;
; Opt. Inputs : None.
;
; Outputs : None.
;
; Opt. Outputs: STEPS = Values at which histogram is taken. Each value
; represents histogram between STEP(I) and STEP(I+1).
; HISTO = Histogram values.
;
; Keywords : DELTA = Distance between histogram steps. If not passed,
; then FORM_HISTO chooses a suitable value.
; TITLE = Main plot title, default is !P.TITLE.
; XTITLE = X axis title, default is !X.TITLE.
; YTITLE = Y axis title, default is !Y.TITLE.
; XTYPE = If set, then X axis is logarithmic.
; YTYPE = If set, then Y axis is logarithmic.
; BCOLOR = Color to use instead of !BCOLOR for bar interiors.
; XRANGE = Range of data values in the X direction.
; YRANGE = Range of data values in the Y direction.
;
; Calls : FORM_HISTO, PLOTBAR
;
; Common : None.
;
; Restrictions: None.
;
; Side effects: None.
;
; Prev. Hist. :
; William Thompson Applied Research Corporation
; September, 1987 8201 Corporate Drive
; Landover, MD 20785
;
; William Thompson, December 1991, added keywords XTYPE and YTYPE.
; William Thompson, May 1992, added keywords XRANGE and YRANGE.
;
; History : Version 1, 22-Jan-1996, William Thompson, GSFC
; Incorporated into CDS library
;
; Contact : WTHOMPSON
;-
;
ON_ERROR,2
;
; Check the input parameters.
;
IF N_PARAMS(0) EQ 0 THEN MESSAGE, $
'Syntax: PLOT_HISTO, ARRAY [, STEPS, HISTO ]'
IF N_ELEMENTS(ARRAY) LE 2 THEN MESSAGE, $
' Not enough points to form histogram.'
;
; Check to see if a histogram is warranted.
;
BANG_C = !C
A_MAX = 1.*MAX(ARRAY)
A_MIN = 1.*MIN(ARRAY)
!C = BANG_C
IF A_MAX EQ A_MIN THEN MESSAGE, $
'No histogram generated--all array elements equal to ' + $
STRING(A_MAX)
IF (A_MAX - A_MIN) LT (1E-4 * ABS(A_MIN)) THEN MESSAGE, $
'No histogram generated--data range ' + STRING(A_MIN) + $
' to ' + STRING(A_MAX) + ' too narrow.'
;
; Call FORM_HISTO to form the histogram, and PLOTBAR to plot it.
;
FORM_HISTO,ARRAY,S,HISTO,DELTA=DELTA
IF N_PARAMS(0) GT 2 THEN STEPS = S
PLOTBAR,S,HISTO,TITLE=TITLE,XTITLE=XTITLE,YTITLE=YTITLE,XTYP E=XTYPE, $
YTYPE=YTYPE,BCOLOR=BCOLOR,XRANGE=XRANGE,YRANGE=YRANGE
;
RETURN
END
============================================================ ===================
PRO FORM_HISTO,ARRAY,STEPS,HISTO,DELTA=DELTA
;+
; Project : SOHO - CDS
;
; Name : FORM_HISTO
;
; Purpose : Forms a histogram from the variable ARRAY.
;
; Category : Class4, Graphics
;
; Explanation : Forms a histogram from the variable ARRAY. Decides what the
; coarseness of the histogram should be. Called from PLOT_HISTO.
;
; ARRAY is scaled into a temporary variable to run from zero and
; some reasonable number, depending on the number of elements of
; ARRAY. This number is the coarseness of the histogram,
; i.e. the number of histogram bins. The more elements in ARRAY,
; the larger this number will be. However, it will not exceed
; 100. The HISTOGRAM function is then used on this temporary
; variable.
;
; If the optional parameter DELTA is passed, then FORM_HISTO uses
; this value to determine the spacing of the histogram bins,
; rather than calculating it's own bin spacing as described
; above.
;
; Syntax : FORM_HISTO, ARRAY, STEPS, HISTO
;
; Examples :
;
; Inputs : ARRAY = Array to form histogram from.
;
; Opt. Inputs : None.
;
; Outputs : STEPS = Values at which histogram is taken. Each value
; represents histogram between STEP(I) and STEP(I+1).
; HISTO = Histogram values.
;
; Opt. Outputs: None.
;
; Keywords : DELTA = Distance between histogram steps. If not passed,
; then the routine chooses a suitable value.
;
; Calls :
;
; Common : None.
;
; Restrictions: None.
;
; Side effects: None.
;
; Prev. Hist. :
; William Thompson Applied Research Corporation
; September, 1987 8201 Corporate Drive
; Landover, MD 20785
;
; William Thompson, May 1992, made DELTA a keyword parameter.
; William Thompson, 25 May 1993, changed to call HISTOGRAM with a long
; array to make compatible with OpenVMS/ALPHA
;
; History : Version 1, 22-Jan-1996, William Thompson, GSFC.
; Incorporated into CDS library.
;
; Contact : WTHOMPSON
;-
;
ON_ERROR,2
;
; Check the number of parameters.
;
IF N_PARAMS(0) NE 3 THEN MESSAGE, $
'Syntax: FORM_HISTO, ARRAY, STEPS, HISTO'
;
; Check the number of elements in ARRAY.
;
IF N_ELEMENTS(ARRAY) LE 2 THEN MESSAGE, $
'Not enough points to form histogram'
;
; Get the maximum and minimum values of ARRAY.
;
BANG_C = !C
A_MAX = 1.*MAX(ARRAY)
A_MIN = 1.*MIN(ARRAY)
!C = BANG_C
IF A_MAX EQ A_MIN THEN BEGIN
PRINT,'*** No histogram generated--all elements equal to ' + $
TRIM(A_MAX) + ', name= ARRAY, routine FORM_HISTO.'
RETURN
END ELSE IF (A_MAX - A_MIN) LT (1E-4 * ABS(A_MIN)) THEN BEGIN
PRINT,'*** No histogram generated--range ' + TRIM(A_MIN) + $
' to ' + TRIM(A_MAX) + $
' too narrow, name= ARRAY, routine FORM_HISTO.'
RETURN
ENDIF
;
; If passed, then check the value of DELTA.
;
IF N_ELEMENTS(DELTA) NE 0 THEN BEGIN
IF N_ELEMENTS(DELTA) NE 1 THEN BEGIN
MESSAGE,'DELTA must be scalar'
END ELSE IF DELTA LE 0 THEN BEGIN
MESSAGE,'DELTA must be positive'
ENDIF
;
; If DELTA was not passed, then determine the approximate number of histogram
; levels from the number of elements of ARRAY.
;
END ELSE BEGIN
N = FLOAT(N_ELEMENTS(ARRAY))
N = N < 100. < (7.*ALOG10(N) + N/8.)
;
; Use N to determine the spacing of the histogram levels. Break this number
; down into mantissa and exponent.
;
DELTA = (A_MAX - A_MIN) / (N - 1)
POWER = FIX(ALOG10(DELTA))
IF POWER GT ALOG10(DELTA) THEN POWER = POWER - 1
DELTA = DELTA / 10.^POWER
;
; Ensure that the spacing of the histogram levels is either 1,2 or 5 times
; some power of ten.
;
VAL = [10,5,2]
VALUE = 1
FOR I = 0,2 DO IF VAL(I) GT DELTA THEN VALUE = VAL(I)
DELTA = VALUE * 10.^POWER
;
; If ARRAY is of some integer type (byte, integer or long), then ensure that
; DELTA is at least one.
;
TYPE = SIZE(ARRAY)
TYPE = TYPE(TYPE(0) + 1)
IF ((TYPE EQ 1) OR (TYPE EQ 2) OR (TYPE EQ 3)) THEN $
DELTA = DELTA > 1
ENDELSE
;
; Find the nearest multiple of DELTA which is LE the minimum of ARRAY.
;
I_MIN = LONG(A_MIN / DELTA)
IF I_MIN*DELTA GT A_MIN THEN I_MIN = I_MIN - 1
A_MIN = I_MIN * DELTA
;
; Form the histogram, and the variable STEPS.
;
HISTO = HISTOGRAM(LONG((ARRAY - A_MIN) / DELTA))
STEPS = INDGEN(N_ELEMENTS(HISTO))*DELTA + A_MIN
;
RETURN
END
============================================================ ===================
PRO PLOTBAR,X,Y,TITLE=TITLE,XTITLE=XTITLE,YTITLE=YTITLE, $
BCOLOR=BCOLOR, XTYPE=XTYPE,YTYPE=YTYPE,XRANGE=XRANGE, $
YRANGE=YRANGE
;+
; Project : SOHO - CDS
;
; Name : PLOTBAR
;
; Purpose : Plot a bar graph filled in with a pattern.
;
; Category : Class3, Graphics
;
; Explanation :
;
; Syntax : PLOTBAR, [ X ,] Y
;
; Examples :
;
; Inputs : Y = array of values along the Y axis.
;
; Opt. Inputs : X = optional array of values along the X axis. Y(i) is plotted
; from X(i) to X(i+1), with the last point being plotted over
; a distance equal to its predecessor. The procedure tests
; whether or not the second parameter passed is a vector to
; decide if X was passed. If not passed, then INDGEN(Y) is
; assumed.
;
; If !BCOLOR is nonzero, then it controls the color of the filled
; in region.
;
; Outputs : None.
;
; Opt. Outputs: None.
;
; Keywords : TITLE = Main plot title, default is !P.TITLE.
; XTITLE = X axis title, default is !X.TITLE.
; YTITLE = Y axis title, default is !Y.TITLE.
; XTYPE = If set, then X axis is logarithmic.
; YTYPE = If set, then Y axis is logarithmic.
; BCOLOR = Color to use instead of !BCOLOR for bar interiors.
; XRANGE = Range of data values in the X direction.
; YRANGE = Range of data values in the Y direction.
;
; Calls : POLYFILL
;
; Common : None.
;
; Restrictions: The variables must not be of type string.
;
; Side effects: None.
;
; Prev. Hist. :
; William Thompson Applied Research Corporation
; July, 1986 8201 Corporate Drive
; Landover, MD 20785
;
; William Thompson, December 1991, added keywords XTYPE and YTYPE.
; W.T.T., May 1992, added BCOLOR keyword.
; William Thompson, May 1992, added keywords XRANGE and YRANGE.
; William Thompson, November 1992, modified to be compatible with
; !P.MULTI.
; William Thompson, January 1993, modified to use modern system parameter
; names.
; William Thompson, 30 April 1993, fixed bug with !P.MULTI.
;
; History : Version 1, 22-Jan-1996, William Thompson, GSFC
; Incorporated into CDS library
;
; Contact : WTHOMPSON
;-
;
ON_ERROR,2
;
PSYM = !PSYM
LINETYPE = !LINETYPE
C = !C
;
IF N_PARAMS(0) EQ 1 THEN BEGIN ;Only Y array passed.
XX = FINDGEN(N_ELEMENTS(X))
YY = X
END ELSE IF N_PARAMS(0) EQ 2 THEN BEGIN
XX = X
YY = Y
END ELSE BEGIN
PRINT,'*** PLOTBAR must be called with 1-2 parameters:'
PRINT,' [ X ,] Y'
RETURN
ENDELSE
;
M = N_ELEMENTS(XX) < N_ELEMENTS(YY)
IF M LE 1 THEN BEGIN
PRINT,'*** Not enough points to plot, routine PLOTBAR.'
RETURN
ENDIF
;
; Generate the arrays used in plotting. Fill in X array so that last
; bar has same width as previous one.
;
I = FINDGEN(2*M)/2
X_LAST = 2*XX(M-1) - XX(M-2)
XX = [XX(I),X_LAST,X_LAST]
YY = [0,YY(I),0]
;
!PSYM=0 ;Save system variables.
!LINETYPE = 0
!C = 0
;
; Do the initial plot, keeping track of the setting of !P.MULTI.
;
PMULTI = !P.MULTI
COMMAND = "PLOT,XX,YY"
IF N_ELEMENTS(XTITLE) EQ 1 THEN COMMAND = COMMAND + ",XTITLE=XTITLE"
IF N_ELEMENTS(YTITLE) EQ 1 THEN COMMAND = COMMAND + ",YTITLE=YTITLE"
IF N_ELEMENTS(TITLE) EQ 1 THEN COMMAND = COMMAND + ",TITLE=TITLE"
IF N_ELEMENTS(XTYPE) EQ 1 THEN COMMAND = COMMAND + ",XTYPE=XTYPE"
IF N_ELEMENTS(YTYPE) EQ 1 THEN COMMAND = COMMAND + ",YTYPE=YTYPE"
IF N_ELEMENTS(XRANGE) EQ 2 THEN COMMAND = COMMAND + ",XRANGE=XRANGE"
IF N_ELEMENTS(YRANGE) EQ 2 THEN COMMAND = COMMAND + ",YRANGE=YRANGE"
TEST = EXECUTE(COMMAND)
;
XX = !CXMIN > XX < !CXMAX
YY = !CYMIN > YY < !CYMAX
; IF N_ELEMENTS(BCOLOR) NE 1 THEN BCOLOR = !BCOLOR
IF N_ELEMENTS(BCOLOR) NE 1 THEN BCOLOR = !COLOR
IF BCOLOR EQ 0 THEN COLOR=!COLOR ELSE COLOR=BCOLOR
;
; Fill in histogram and overplot boundary to restore color.
;
POLYFILL,XX,YY,/DATA,COLOR=COLOR
NOERAS = !NOERAS
!NOERAS = -1
XTITLE = !X.TITLE & !X.TITLE = ''
YTITLE = !Y.TITLE & !Y.TITLE = ''
PTITLE = !P.TITLE & !P.TITLE = ''
!P.MULTI = PMULTI
;
COMMAND = "PLOT,XX,YY"
IF N_ELEMENTS(XTYPE) EQ 1 THEN COMMAND = COMMAND + ",XTYPE=XTYPE"
IF N_ELEMENTS(YTYPE) EQ 1 THEN COMMAND = COMMAND + ",YTYPE=YTYPE"
IF N_ELEMENTS(XRANGE) EQ 2 THEN COMMAND = COMMAND + ",XRANGE=XRANGE"
IF N_ELEMENTS(YRANGE) EQ 2 THEN COMMAND = COMMAND + ",YRANGE=YRANGE"
TEST = EXECUTE(COMMAND)
;
; Modify !P.MULTI. Normally, this would happen automatically, but this has
; been disabled for this routine.
;
K = !P.MULTI(0)
NX = !P.MULTI(1) > 1
NY = !P.MULTI(2) > 1
IF (K LE 0) OR (K GT NX*NY) THEN K = NX*NY
!P.MULTI(0) = (K - 1) MOD (NX*NY)
;
!NOERAS = NOERAS
!X.TITLE = XTITLE & !Y.TITLE = YTITLE & !P.TITLE = PTITLE
OPLOT,[!CXMIN,!CXMAX],[0,0] ;Plot zero line.
;
!PSYM = PSYM
!LINETYPE = LINETYPE
!C = C
;
RETURN
END
Re: Question on plotting a histogram with original values [message #8132 is a reply to message #8129] Thu, 06 February 1997 00:00 Go to previous message
davidf is currently offline  davidf
Messages: 2866
Registered: September 1996
Senior Member
David van Kuijk <kuijk@mpi.nl> writes:

> When I use the histogram-function I find it very annoying that I can't see
> which range of data has been put in which bin. Has anybody written a
> function which returns these values, or the mean values for each bin?
>
> To give an example:
> PRO testHisto
>
> testAr = [-44,0,-42,-58,-48,-25,-26,-1,-46,-36,-16,-55,-6,-81,10,-31, -38,$
>
-34,-49,-80,2,-40,-65,-3,10,-31,-85,-34,-27,-14,-41,-73,-67, 3,-51,$
> -6,0,-107,-7,-36,-33,-59,-2,-99,-22]
> testHisto = histogram(testAr, BINSIZE = 10)
> plot, testHisto, PSYM = 10
> END
>
> This program will plot a nice histogram, but the values on the x-axis just
> show the rank number of the bins, and the information on the ranges of the
> original data is lost.

Try this modification to your program, David.

PRO testHisto

testAr = [-44,0,-42,-58,-48,-25,-26,-1,-46,-36,-16,-55,-6,-81,10,-31, -38,$

-34,-49,-80,2,-40,-65,-3,10,-31,-85,-34,-27,-14,-41,-73,-67, 3,-51,$
-6,0,-107,-7,-36,-33,-59,-2,-99,-22]
testHisto = histogram(testAr, BINSIZE = 10)
s = SIZE(testHisto)
maxData = MAX(testAr, MIN=minData)
x = FINDGEN(s(1)) * ((maxData - minData)/(s(1)-1)) + minData
plot, x, testHisto, PSYM = 10, XSTYLE=1
END

Cheers!

David

-----------------------------------------------------------
David Fanning, Ph.D.
Fanning Software Consulting
2642 Bradbury Court, Fort Collins, CO 80521
Phone: 970-221-0438 Fax: 970-221-4762
E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com
-----------------------------------------------------------
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: changing colors on a Mac
Next Topic: IDL 5.0 news at RSI website

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

Current Time: Fri Oct 10 16:00:45 PDT 2025

Total time taken to generate the page: 1.44236 seconds