This is graphics routine to display a box plot, also known as a box and
whisker plot, in IDL direct graphics. The box encloses the interquartile
range (IQR), defined at IQR75-IQR25. The whiskers extend out to the maximum
or minimum value of the data, or to the 1.5 times either the IQR75 or IQR25,
if there is data beyond this range. Outliers are identified with small circles.
Here is an example, using data from the Michaelson-Morley speed of light experiment,
in which they made five experiments of 20 measurements of the speed of light each.
The data can be downloaded from here::
Here are the IDL commands to read the data and produce a box plot of it::
OpenR, 1, Find_Resource_File('mm_data.dat')
header = Strarr(2)
Readf, 1, header
data = Intarr(5, 20)
Readf, 1, data
cgBoxPlot, data, XTITLE='Experiment Number', YTITLE='Speed of Light'
Here is an example that produces a low, medium, and high box for each of
six experiments and plots them::
data = fltarr(18, 40)
index = indgen(6)*3
for j=0,5 do data[index[j],*] = Randomu(seed, 40)*6
index = index+1
for j=0,5 do data[index[j],*] = Randomu(seed, 40)*10
index = index+1
for j=0,5 do data[index[j],*] = Randomu(seed, 40)*15
cgPlot, [0,1], /nodata, yrange=[0,16], xrange=[0,19], $
xtickformat='(A1)', ytitle='Gc(mms-1)', YStyle=1
index = indgen(6)*3
width = ((!X.CRange - !X.Crange) / (20)) * 0.75
cgBoxPlot, data[index, *],/overplot, XLOCATION=index+1, WIDTH=width, $
cgBoxPlot, data[index+1, *],/overplot, XLOCATION=index+2, WIDTH=width, $
BOXCOLOR='pale green', /FILLBOX
cgBoxPlot, data[index+2, *],/overplot, XLOCATION=index+3, WIDTH=width, $
labels = ['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF']
for j=0,5 do cgText, (index+2)[j], -1, labels[j], Alignment=0.5
An article about his program can be found here::
FANNING SOFTWARE CONSULTING::
David W. Fanning
1645 Sheely Drive
Fort Collins, CO 80526 USA
Coyote's Guide to IDL Programming: http://www.idlcoyote.com
Written by David W. Fanning, 4 March 2009.
Added STATS keyword to return data statistics. 5 March 2009. DWF.
Added MISSING_DATA_VALUE keyword to identify missing values. 14 March 2009. DWF.
Removed limitation of LABELS array having no more than 28 elements. 14 March 2009. DWF.
Made it possible to pass a pointer array containing the data, if desired. 14 March 2009. DWF.
Added ROTATE keyword to rotate labels. 16 March 2009. DWF.
Added several modifications to guard against ill-formed data in the cgBoxPlot_Draw
procedure. 23 March 2009. DWF.
Added keywords FILLBOXES and BOXCOLOR. 24 March 2009. DWF.
Redefined the STATS structure to include MEAN and to store values as doubles. 25 March 2009. DWF.
Fixed in a bug that resulted in incorrect behavior when the MISSING_DATA_VALUE keyword
was used. 8 April 2009. DWF.
Fixed a typo that didn't allow a single column vector to be displayed as a box plot. 17 May 2009. DWF.
Now allow a single row vector to be passed into program and displayed. 20 May 2009. DWF.
Added NOCLIP=0 keyword to PLOTS command when drawing outliers. 15 July 2009. DWF.
Minor adjustment of the X axis label position. 28 October 2010. DWF.
Add the ability to change the label character size and thickness via the normal
XCHARSIZE and XTHICK keywords you would use for a plot. 3 Dec 2010. DWF.
Fixed a couple of typos, added ADDCMD, CHARSIZE, LAYOUT and WINDOW keywords. 2 Feb 2011. DWF.
Added the ability to send the output directly to a file via the OUTPUT keyword. 9 Dec 2011, DWF.
PostScript, PDF, and Imagemagick parameters can now be tailored with cgWindow_SetDefs. 14 Dec 2011. DWF.
Added XLOCATION and WIDTH keywords. 5 June 2012. DWF.
Copyright (c) 2009, Fanning Software Consulting, Inc.