JMSL Chart Programmer's Guide
Charting 2D Types >> Box Plot  Previous Page  Contents  Next Page

Box Plot

Box plots are used to show statistics about multiple groups of observations.

For each group of observations, the box limits represent the lower quartile (25-th percentile) and upper quartile (75-th percentile). The median is displayed as a line across the box. Whiskers are drawn from the upper quartile to the upper adjacent value, and from the lower quartile to the lower adjacent value.

Optional notches may be displayed to show a 95 percent confidence interval about the median, at

where IRQ is the interquartile range and n is the number of observations. Outside and far outside values may be displayed as symbols. Outside values are outside the inner fence. Far out values are outside the outer fence.

The BoxPlot has several child nodes. Any of these nodes can be disabled by setting their "Paint" attribute to false.

Example

In this example, the Fisher iris data set is read from a file and a Box plot is created from data. The data is in a file called "FisherIris.csv" (see Attribute MarkerSize) in the same directory as this class.

The y-axis labels are taken from the column names.

The boxes are colored green, the markers are all filled circles. The outside markers are blue and the far outside markers would be red, if there were any.











(Download Code)
import com.imsl.chart.*;
import com.imsl.io.FlatFile;
import java.io.*;
import java.sql.SQLException;
import java.util.StringTokenizer;


public class SampleBoxPlot extends JFrameChart {
    
    public SampleBoxPlot() throws IOException, java.sql.SQLException {        
        // Read the data
        InputStream is = SampleBoxPlot.class.getResourceAsStream("FisherIris.csv");
        DataReader reader = new DataReader(is);
        int nColumns = 5;
        int nObs = 150;
        String labels[]  = new String[nColumns];
        for (int i = 0;  i < nColumns;  i++) {
            labels[i] = reader.getMetaData().getColumnName(i+1);
        }
        double irisData[][] = new double[nColumns][nObs];
        for (int j = 0;  reader.next();  j++) {
            for (int i = 0;  i < nColumns;  i++) {
                irisData[i][j] = reader.getDouble(i+1);
            }
        }

        // Setup the chart
        Chart chart = getChart();
        AxisXY axis = new AxisXY(chart);
        BoxPlot boxPlot = new BoxPlot(axis, irisData);
        boxPlot.setBoxPlotType(BoxPlot.BOXPLOT_TYPE_HORIZONTAL);
        boxPlot.setLabels(labels);
        boxPlot.getBodies().setFillColor("green");
        boxPlot.setMarkerType(BoxPlot.MARKER_TYPE_FILLED_CIRCLE);
        boxPlot.getOutsideMarkers().setMarkerColor("blue");
        boxPlot.getFarMarkers().setMarkerColor("red");
        boxPlot.setNotch(true);
    }
    
    
    public static void main(String argv[]) throws IOException, java.sql.SQLException {
        new SampleBoxPlot().setVisible(true);
    }
    
    
    /**
     *  Read the Fisher Iris data
     */
    static private class DataReader extends FlatFile {        
        public DataReader(InputStream is) throws IOException {
            super(new BufferedReader(new InputStreamReader(is)));
            String line = readLine();
            StringTokenizer st = new StringTokenizer(line, ",");
            for (int j = 0;  st.hasMoreTokens();  j++) {
                setColumnName(j+1, st.nextToken().trim());
                setColumnClass(j, Double.class);
            }
        }
    }
}

(Download FisherIris.csv)
Species,Sepal Length,Sepal Width,Petal Length,Petal Width
 1.0, 5.1, 3.5, 1.4, .2
 1.0, 4.9, 3.0, 1.4, .2
 1.0, 4.7, 3.2, 1.3, .2
 1.0, 4.6, 3.1, 1.5, .2
 1.0, 5.0, 3.6, 1.4, .2
 1.0, 5.4, 3.9, 1.7, .4
 1.0, 4.6, 3.4, 1.4, .3
 1.0, 5.0, 3.4, 1.5, .2
 1.0, 4.4, 2.9, 1.4, .2
 1.0, 4.9, 3.1, 1.5, .1
 1.0, 5.4, 3.7, 1.5, .2
 1.0, 4.8, 3.4, 1.6, .2
 1.0, 4.8, 3.0, 1.4, .1
 1.0, 4.3, 3.0, 1.1, .1
 1.0, 5.8, 4.0, 1.2, .2
 1.0, 5.7, 4.4, 1.5, .4
 1.0, 5.4, 3.9, 1.3, .4
 1.0, 5.1, 3.5, 1.4, .3
 1.0, 5.7, 3.8, 1.7, .3
 1.0, 5.1, 3.8, 1.5, .3
 1.0, 5.4, 3.4, 1.7, .2
 1.0, 5.1, 3.7, 1.5, .4
 1.0, 4.6, 3.6, 1.0, .2
 1.0, 5.1, 3.3, 1.7, .5
 1.0, 4.8, 3.4, 1.9, .2
 1.0, 5.0, 3.0, 1.6, .2
 1.0, 5.0, 3.4, 1.6, .4
 1.0, 5.2, 3.5, 1.5, .2
 1.0, 5.2, 3.4, 1.4, .2
 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