Customizing Histograms

Useful hints
Post Reply
User avatar
joleenf
Posts: 1123
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 java.io.File as File
import java.lang.Double as Double
import org.jfree.data.statistics as jfreeStats
import org.jfree.chart as jfreeCharts

homeDir=os.path.expanduser('~')

# get the data from the
a=selectData()

# 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
jfreeCharts.ChartUtilities.saveChartAsPNG(newFile,myHist,600,600)

print "Done:  saved as " + outFile


The result is

testJFREE.png


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.

Joleen
User avatar
joleenf
Posts: 1123
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)")
myHist=jfreeCharts.ChartFactory.createHistogram(myTitle,domainTitle,"frequency",newHD,jfreeCharts.plot.PlotOrientation.VERTICAL,0,0,0)


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
   jfreeCharts.ChartUtilities.saveChartAsPNG(newFile,myHist,960,720)     


The result

AGOES13_ALL_2013-06-01_064500_hist.png


Joleen
Post Reply