Customizing Histograms

Useful hints
Post Reply
User avatar
Posts: 1120
Joined: Mon Jan 19, 2009 7:16 pm

Customizing Histograms

Post by joleenf »

Recently I have been working on running statistics using the libraries from McIDAS-V and Hydra in scripts. I needed to create histograms of the data. Since JFreeChart is packaged in the IDV/McIDAS-V external.jar file, I was able to use the JFreeChart API to create some simple histograms. The example below is done within McIDAS-V via the jython shell, but I have incorporated the technique in a script which is running in the background.

Code: Select all

import os

import as File
import java.lang.Double as Double
import as jfreeStats
import org.jfree.chart as jfreeCharts


# get the data from the

# some options for creating the histogram.  Set to False.
show = 0;
toolTips = 0;
urls = 0;

# Initialize a dataset that can be used for creating histograms in JFreeChart
# The default type is HistogramType.FREQUENCY

newHD = jfreeStats.HistogramDataset()

# establish a low and high bin for the plot
lowBin = Double(180)
highBin = Double(360)

# Get the doubles array from the visad object captured with selectData()
myArray = (a[0].getValues())[0]

# add a series to the HistogramDataset object “newHD.”  This will contain the array
# of doubles, 16 bins, and the low/high bins defined.
newHD.addSeries("H1",myArray,20, lowBin, highBin)

# create the histogram.
myHist=jfreeCharts.ChartFactory.createHistogram("My Title","3.9 micron Temperature (K)","Frequency",newHD,jfreeCharts.plot.PlotOrientation.VERTICAL,show,toolTips,urls)

# create a new java file object for saving the image
outFile = homeDir + "/testJFREE.png"
newFile = File(outFile)

#save the chart

print "Done:  saved as " + outFile

The result is


This is a partial tips and tricks. There are many options which I don't know how to use fully and other questions that I have. I hope that this can be expanded as I or others learn more.

User avatar
Posts: 1120
Joined: Mon Jan 19, 2009 7:16 pm

Re: Customizing Histograms

Post by joleenf »

How do I add a greek character? JFreeChart uses java strings, so the unicode value can be used. For example:

Code: Select all

import java.lang.String as javaString;
domainTitle = javaString("3.9 - 10.7" + u'\u03bc' + "m Brightness Temperature Difference (K)")

Next, how do I change the tick units (example will be for the domain? First, get the plot object, get the domain axis, set the tick units by distance between units. Save the chart

Code: Select all

   # Change the default tick unit...
   plot = myHist.getPlot()                             # get the histogram plot
   domainAxis = plot.getDomainAxis()         # get the control for the domain axis

   # Create a tickUnit object
   tickUnit = jfreeCharts.axis.NumberTickUnit(float(5))      # determine the tick unit spacing
   domainAxis.setTickUnit(tickUnit)                                   # apply this spacing to the domain axis
   # save chart

The result


Post Reply