Histogram

A somewhat more specialized class is Histogram and its support classes HistogramApplet, HistogramMLApplet, and HistogramMLApplication. The Histogram class is not just a plotter. It actually calculates the histogram from raw data. Data can be provided by Java code (by extending any of the support classes above), by a PlotML file, or by an ASCII file in a simple format described below. Here is an example of a histogram:

The above applet shows three histograms. The one in red is a histogram of a cosine signal with amplitude 5.0, showing that fewer values lie in the ranges in the middle than near the amplitude extremes. Each bar is centered in the middle of a histogram bin. The height of each bar represents the number of samples that lie within that bin. The bar is not as wide as the bin so that multiple histograms show at once.

The blue histogram represents 1000 random numbers in the range (-5.0, 5.0). The cyan histogram represents 1000 Gaussian random numbers with mean zero and standard deviation 2.0.

The above histogram data is generated by Java code (in the samplePlot() method of the Histogram class). An example of the specification of the data in a PlotML file is:

    <?xml version="1.0" standalone="yes"?>
    <!DOCTYPE plot PUBLIC "-//UC Berkeley//DTD PlotML 1//EN"
	"http://ptolemy.eecs.berkeley.edu/xml/dtd/MoML_1.dtd">
    <plot>
        <title>Sample histogram</title>
        <xLabel>values</xLabel>
        <yLabel>count</yLabel>
        <yRange min="0.0" max="100.0"/>
        <barGraph width="0.5" offset="0.15"/>
        <bin width="1.0" offset="0.5"/>
        <dataset name="first">
            <p y="5.0">
            <p y="4.9999013042806855">
            <p y="4.99960522101908">
            ...
        </dataset>
        <dataset name="second">
            <p y="1.424032891368717">
            <p y="3.6593908462220632">
            ...
        </dataset>
    </plot>

In this syntax, the look and labeling of the plot is specified exactly as with other plots. In fact, the syntax is readable by the PlotMLParser class, so it is easy to plot the raw data and the histogram from the same specification file. Here is an example of a histogram that reads PlotML data from a URL:

The data here is exactly that of the sinusoids demo, but in PlotML format.

Here is an example of data in the simpler ASCII syntax:

    TitleText: Sample histogram
    XLabel: values
    YLabel: count
    YRange: 0.0,100.0
    BarGraph: 0.5,0.15
    BinWidth: 1.0
    BinOffset: 0.5
    Dataset: first
    5.0
    4.9999013042806855
    4.99960522101908
        ...
    Dataset: second
    1.424032891368717
    3.6593908462220632
        ...

This latter syntax is probably more convenient for interfacing to arbitrary programs that generate data, since the simple list of numbers is often easy to produce.

Either syntax can be simply put in a file and referenced by the dataurl applet parameter of HistogramMLApplet (HistogramApplet can only read the latter syntax).