JMSL Chart Programmer’s Guide
Error Bar Plot
Error bars are used to indicate the estimated error in a measurement. Errors bars indicate the uncertainty in the x and/or y-values.
Vertical Error Bars
The most common error bar plot is one in which the errors are in the y-values. This example shows such an error bar plot. Note that the values of the low and high arguments are absolute y-values, not relative or percentage values.
View code file
 
import com.imsl.chart.*;
import java.awt.Color;
import com.imsl.stat.Random;
 
public class SampleErrorBar extends JFrameChart {
 
public SampleErrorBar() {
Random r = new Random(123457);
Chart chart = getChart();
AxisXY axis = new AxisXY(chart);
// Generate a random data set, with random errors
int n = 20;
double x[] = new double[n];
double y[] = new double[n];
double low[] = new double[n];
double high[] = new double[n];
for (int k = 0; k < n; k++) {
x[k] = k + 1;
y[k] = r.nextDouble();
low[k] = y[k] - 0.25*r.nextDouble();
high[k] = y[k] + 0.25*r.nextDouble();
}
ErrorBar data = new ErrorBar(axis, x, y, low, high);
data.setDataType(data.DATA_TYPE_ERROR_Y | data.DATA_TYPE_MARKER);
data.setMarkerType(Data.MARKER_TYPE_FILLED_CIRCLE);
data.setMarkerColor(Color.red);
}
public static void main(String argv[]) {
new SampleErrorBar().setVisible(true);
}
}
Horizontal Error Bars
It is also possible to have horizontal error bars, indicating errors in x, as shown in this example.
View code file
 
import com.imsl.chart.*;
import java.awt.Color;
import com.imsl.stat.Random;
 
public class SampleHorizontalErrorBar extends JFrameChart {
 
public SampleHorizontalErrorBar() {
Random r = new Random(123457);
Chart chart = getChart();
AxisXY axis = new AxisXY(chart);
// Generate a random data set, with random errors in x
int n = 20;
double x[] = new double[n];
double y[] = new double[n];
double low[] = new double[n];
double high[] = new double[n];
for (int k = 0; k < n; k++) {
x[k] = k;
y[k] = r.nextDouble();
low[k] = x[k] - 5.*r.nextDouble();
high[k] = x[k] + 5.*r.nextDouble();
}
ErrorBar data = new ErrorBar(axis, x, y, low, high);
data.setDataType(
ErrorBar.DATA_TYPE_ERROR_X | ChartNode.DATA_TYPE_MARKER);
data.setMarkerType(Data.MARKER_TYPE_FILLED_CIRCLE);
data.setMarkerColor(Color.red);
}
public static void main(String argv[]) {
new SampleHorizontalErrorBar().setVisible(true);
}
}
Mixed Error Bars
To show errors in both x and y, it is necessary to create both vertical and horizontal error bar objects. This example shows such a chart.
View code file
 
import com.imsl.chart.*;
import java.awt.Color;
import com.imsl.stat.Random;
 
public class SampleMixedErrorBar extends JFrameChart {
 
public SampleMixedErrorBar() {
Random r = new Random(123457);
Chart chart = getChart();
AxisXY axis = new AxisXY(chart);
// Generate a random data set, with random errors in x
int n = 20;
double x[] = new double[n];
double y[] = new double[n];
double xlow[] = new double[n];
double xhigh[] = new double[n];
double ylow[] = new double[n];
double yhigh[] = new double[n];
for (int k = 0; k < n; k++) {
x[k] = k;
y[k] = r.nextDouble();
xlow[k] = x[k] - r.nextDouble();
xhigh[k] = x[k] + r.nextDouble();
ylow[k] = y[k] - 0.25*r.nextDouble();
yhigh[k] = y[k] + 0.25*r.nextDouble();
}
ErrorBar dataY = new ErrorBar(axis, x, y, ylow, yhigh);
dataY.setDataType(
ErrorBar.DATA_TYPE_ERROR_Y | ChartNode.DATA_TYPE_MARKER);
dataY.setMarkerType(Data.MARKER_TYPE_FILLED_CIRCLE);
dataY.setMarkerColor(Color.red);
ErrorBar dataX = new ErrorBar(axis, x, y, xlow, xhigh);
dataX.setDataType(ErrorBar.DATA_TYPE_ERROR_X);
dataX.setMarkerColor(Color.red);
}
public static void main(String argv[]) {
new SampleMixedErrorBar().setVisible(true);
}
}