Skip to content

Histograms

Will Phelps edited this page May 5, 2017 · 8 revisions

Basic histogram

Here's a basic example showing you how to create and fill a gaussian histogram. This example uses a standard Swing Frame EmbeddedCanvas on the UI side. The EmbeddedCanvas inherits from JPanel, so you are able to put it into JTabbedPanes and do anything that you'd normally do with a JPanel

import java.util.Random;
import javax.swing.JFrame;

import org.jlab.groot.data.H1F;
import org.jlab.groot.graphics.EmbeddedCanvas;

public class BasicDemo {

	public static void main(String[] args) {
		JFrame frame = new JFrame("Basic GROOT Demo");
		EmbeddedCanvas canvas = new EmbeddedCanvas();
		frame.setSize(800,500);
		H1F histogram = new H1F("histogram",100,-5,5); 
		Random randomGenerator = new Random();
		for(int i=0; i<5000; i++){
			histogram.fill(randomGenerator.nextGaussian());
		}
		canvas.draw(histogram);
		frame.add(canvas);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}

}

Setting Titles

But that doesn't look too great. So we should set some titles.

histogram.setTitleX("Randomly Generated Function");
histogram.setTitleY("Counts");

But what about the Pad title? That's a part of the EmbeddedPad object.

canvas.getPad(0).setTitle("BasicDemo Test");

Setting colors and line thickness

Colors and other attributes are part of the DatasetAttributes object, but are accessible through methods in the different plot types as show below.

histogram.setLineWidth(2);
histogram.setLineColor(21);
histogram.setFillColor(34);

Setting font sizes and font!

The simplest way to set the font (before we roll out a Style package) is to set the font size and font for the entire canvas (including all the pads) all at once.

canvas.setFont("HanziPen TC");  
canvas.setTitleSize(32);
canvas.setAxisTitleSize(24);
canvas.setAxisLabelSize(18);
canvas.setStatBoxFontSize(18);

StatBox Options

You can now SetOptStat for each individual dataset and the pad will construct a stat box showing the selected items.

canvas.setStatBoxFontSize(18);
histogram.setOptStat(1110);

//Note: if you setOptStat(1110) for the pad, it will only apply to the first dataset

Final Result:

Setting Log Scale

canvas.getPad(0).getAxisY().setLog(true);
canvas.getPad(0).getAxisZ().setLog(true);

//Note: It is highly recommended that you don't rely on autoscale for log scale axes