PRO bargraph, YY, sigma=sigma, $ minbin=minbin, maxbin=maxbin, bin=bin, histo=histo, $ hatch=hatch, shade_color=shade_color, xrange=xrange, yrange=yrange, title=title, $ average=average, median=median, maximum=maximum, $ _extra=_extra, stat=stat, oplotx=oplotx ;+ ; NAME: ; bargraph ; PURPOSE: ; Plot bar graph ; CATEGORY: ; Plotting ; CALLING SEQUENCE: ; bargraph, Y, sigma=sigma, $ ; minbin=minbin, maxbin=maxbin, bin=bin, histo=histo, $ ; /hatch, xrange=xrange, yrange=yrange, title=title ; /average, /median, /maximum ; INPUTS: ; Y array[n]; type: any numerical array ; array to be plotted in bargraph ; OPTIONAL INPUTS: ; sigma=sigma scalar or array[n]; type: float ; errors in Y-array ; Ignored if /histo is set. ; If sigma is not a scalar, but does not have the ; same # elements as Y then it is ignored ; /histo if set the Y is passed through the IDL ; histogram function using keywords minbin, maxbin, bin ; minbin scalar; type: float; default: 0.0 ; minimum X-value = left edge of leftmost bin ; bin scalar; type: float; default: 1.0 ; bin width ; maxbin scalar; type: float ; maximum Y-value, only used as keyword to ; IDL histogram function if /histo is set ; ; /hatch if set and nonzero, bins will be hatched ; xrange=xrange ; array[2]; type: float ; xrange keyword to IDL plot command ; yrange=yrange ; array[2]; type: float ; yrange keyword to IDL plot command ; /average if set, plot vertical line at position of average ; /median if set, plot vertical line at position of median ; /maximum if set, plot vertical line at position of maximum ; title scalar; type: string; default: value of bin width ; title string ; _extra=_extra additional keywords passed to IDL plot command ; OPTIONAL OUTPUTS: ; stat=stat array[3]; type: float ; positions of average, median and maximum ; INCLUDE: @compile_opt.pro ; On error, return to caller ; CALLS: ; InitVar, statpos, IsType ; PROCEDURE: ; > The number of bins is n_elements(Y) ; > The bin edges are minbin+bin*indgen(n_elements(Y)+1) ; > To plot a histogram of an array ; bargraph,histogram(Y,min=25,max=350,bin=50),25,50 ; MODIFICATION HISTORY: ; AUG-1992, Paul Hick (UCSD/CASS) ; Modification of HIST.PRO (Written Feb'91 by DMZ (ARC)) ; NOV-1992, Paul Hick (UCSD/CASS) ; Added option to plot standard deviations ; JAN-2000, Paul Hick (UCSD/CASS; pphick@ucsd.edu) ; Minor modifications; added stat keyword ;- InitVar, oplotx, /key InitVar, hatch, /key InitVar, histo, /key shade = n_elements(shade_color) NE 0 InitVar, average, /key InitVar, median , /key InitVar, maximum, /key InitVar, minbin , 0.0 InitVar, bin , 1.0 ; The plus 0.5 works better for floating point arrays. ; For integer arrays the bins need to start and end at half integer ; values, otherwise funny things happen CASE histo OF 0: stat = minbin+bin*(statpos(YY, hist=Y)+0.5) 1: stat = statpos(YY, hist=Y, min=minbin, max=maxbin, bin=bin) ENDCASE NY = n_elements(Y) IF NY EQ 0 THEN message, 'Usage: bargraph, y' isdev = NOT histo AND IsType(sigma, /defined) IF isdev THEN BEGIN IF n_elements(sigma) EQ 1 THEN sigma = replicate(sigma[0], NY) isdev = n_elements(sigma) EQ NY CASE isdev OF 0: message, 'Usage: bargraph, y, sigma=sigma (with same size arrays)' 1: isdev = max(sigma) NE 0 ENDCASE ENDIF Z = minbin+bin*findgen(NY+1) ; Bin edges ;-- set up plot window InitVar, title, 'BIN WIDTH: '+string(bin,'(F6.2)') InitVar, xrange, [min(Z),max(Z)] xstyle = 1 ; Exact scaling ystyle = 1-IsType(yrange, /defined) ; Extend axis range, unless range specified IF ystyle EQ 1 THEN BEGIN p = where(xrange[0] LE Z AND Z LE xrange[1]) IF isdev THEN yrange = [0,1.1*max(Y[p]+sigma[p])] ELSE yrange = [0,1.1*max(Y[p])] ENDIF ;-- plot window for bar chart IF NOT oplotx THEN $ plot, xrange, yrange, /nodata, xstyle=xstyle, ystyle=ystyle, $ title=title, noclip=0, _extra=_extra FOR i=0L,NY-1 DO BEGIN p = [Z[i]*[1,1],Z[i+1]*[1,1]] q = Y[i]*[0,1,1,0] oplot, p,q, noclip=0 IF shade NE 0 AND Y[i] NE 0 THEN $ polyfill,p,q,color=shade_color ,noclip=0 IF hatch AND Y[i] NE 0 THEN $ polyfill,p,q,spacing=hatch,orientation=([45,-45])[i mod 2] ,noclip=0 ENDFOR Yp = yrange[1]-0.025*(yrange[1]-yrange[0]) Xp = 0.01*(xrange[1]-xrange[0]) p = [average, median, maximum] IF total(p) NE 0 THEN plotstat = where(p) FOR i=0,n_elements(plotstat)-1 DO BEGIN q = plotstat[i] p = stat[q] oplot, p*[1,1], yrange, linestyle=2 xyouts, p-Xp, Yp, (['AVERAGE','MEDIAN','MAXIMUM'])[q], orientation=90.0, align=1.0 ENDFOR FOR i=0L,isdev*NY-1 DO BEGIN ; Plot errors p = 0.5*(Z[i]+Z[i+1]) oplot, p*[1,1], Y[i]+sigma[i]*[-1,1], noclip=0 dp = 0.15*(Z[i+1]-Z[i]) q = Y[i]+sigma[i] oplot, p+dp*[-1,1], q*[1,1], noclip=0 q = Y[i]-sigma[i] oplot, p+dp*[-1,1], [q,q], noclip=0 ;p = [Z[i]*[1,1],Z[i+1]*[1,1],Z[i]] ;q = Y[i]+sigma[i]*[-1,1,1,-1,-1] ;oplot, p,q, noclip=0 ;if Y[i] ne 0 and sigma[i] ne 0 then $ ; polyfill,p,q,spacing=0.5*hatch,orientation=ORIENT(1-(i mod 2)),noclip=0 ENDFOR RETURN & END