To construct a histogram representing a density distribution of some variable one should follow these 2 steps: construct a histogram object using the H1D class and then fill it.
This is an example using the JHPLOT package (here we are using the Jython syntax instead of Java):
from java.util import Random from jhplot import * c1 = HPlot("Canvas") c1.setGTitle("A histogram") c1.visible() c1.setAutoRange() h1 = H1D("1D histogram",100, -2, 2) rand = Random() for i in range(100): h1.fill(rand.nextGaussian()) c1.draw(h1) c1.drawStatBox(h1)
The output with a statistical summary is plotted as well (the method drawStatBox). By default, the plot shows statistical uncertainties in each bin.
In the above example, “100” is the number of bins between -2 and 2, thus all bins are of the same size. You can get some information about histograms as:
See more details in the jHepWork H1D API
Build a histogram in 2 dimensions using the Java class H2D. This is an example using the JHPLOT package (here we are using again Jython syntax, instead of Java):
from java.util import Random from jhplot import * # build a standard 3D canvas c1 = HPlot3D("Canvas") c1.setGTitle("Global title") c1.setNameX("X") c1.setNameY("Y") c1.visible(1) h1 = H2D("2D histogram",10,-3.0, 3.0,10,-3.0, 3.0) rand = Random(); for i in range(200): h1.fill(rand.nextGaussian(),rand.nextGaussian()) c1.draw(h1);
The output with statistical summary is shown here. By default, the plot shows statistical uncertainties in each bin.
Similarly, histograms can be defined in 3D using the class H3D
One can use also variable-size bins as:
h1 = H1D("Variable-size bins",[-2,-1,0,2,10])
where the list used in the H1D constructor specifies edges of the bins. Similarly, one can define H2D and H3D histogram by passing 2 lists (one for X, one for Y) or 3 lists (X,Y,Z).
The histogram classes support many mathematical operations (division, subtraction, multiplication, scaling, shifting, smoothing etc). Histogram arithmetic can be done with the method “oper(h,”New Title”,”operation”)”, where “h” is an object represented a histogram which is used to subtract, divide, multiply and add. All these operations should be defined by a string operation as ”-, /, *, +”, and the histograms must have the same binning. It should also be noted that all such operations take into account propagation of statistical errors for each bin assuming that histograms do not correlate.
from java.util import Random
from jhplot import *
h1 = H1D("First",10, -2.0, 2.0)
h2 = H1D("Second",10, -2.0, 2.0)
r = Random()
for i in range(5000): h1.fill(r.nextGaussian())
for i in range(5000): h2.fill(r.nextGaussian())
h3=h1.oper(h2,"subtract","-")
h4=h1.oper(h2,"add","+")
h5=h1.oper(h2,"multiply","*")
h6=h1.oper(h2,"divide","/")
A histogram can be scaled by a constant using the method “operScale(title,scaleFactor)”
Instead of calling Java classes using the Jython (or Python) language, one can use the native Jython classes based on the Java classes. In this case, many Java methods can conveniently be overloaded. For example, histograms can be added, subtracted, divided and multiplied using the conventional arithmetic operators ”+,-,/,*”. To be able to use Python-derived classes for the histogram objects, import the class “shplot” (“scripting” HPlot package). The histogram classes have the same names, but they start from a lower case. Let us give an example:
import shplot from java.awt import Color from java.util import Random from shplot import * print shplot.__doc__ c1=hplot("scripting",1,2) # build a canvas with 2 plot regions c1.visible() c1.auto() h1=h1d("h1",40,-3,3) # define histograms h2=h1d("h2",40,-3,3) h3=h1d("h3",40,-3,3) r = Random() for i in range(2000): # fill histograms h1.fill(r.nextGaussian()) h2.fill(0.6*r.nextGaussian()+2) if i<1000: h3.fill(0.5*r.nextGaussian()+1.0) h1.setColor(Color.red) c1.draw(h1) h12=h1+h2 # add 2 histograms h12.setFill(1) h12.setFillColor(Color(20,30,20)) h12.setColor(Color.blue) c1.draw(h12) h13=h12+h3 # sum 2 histogram and draw h13.setFill(1) h13.setFillColor(Color(50,90,20)) c1.draw(h13) scaled=h1*2.5 # scale a histogram by 2.5 scaled.setColor(Color.green) c1.draw(scaled) c1.cd(1,2) # create a new plotting region c1.auto() h13=h1+h3 # draw the sum of 2 histograms h13.setColor(Color.blue) c1.draw(h13) h113=h1-h3 # subtract 2 histograms h113.setFill(1) h113.setColor(Color(10,200,100)) h113.setFillColor(Color(20,200,90)) c1.draw(h113)
The resulting figure is shown here