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);
            }
        }
    }
}



©  Visual Numerics, Inc.  All rights reserved.  Previous Page  Contents  Next Page