A discussion forum for JFreeChart (a 2D chart library for the Java platform).
-
stledger
- Posts: 14
- Joined: Sun Mar 09, 2008 7:39 am
Post
by stledger » Thu Jan 09, 2014 4:50 am
This demos how to use the probability axis to plot log-normally distributed data
Code: Select all
/* -------------------
* LineChartDemo6.java
* -------------------
* (C) Copyright 2004-2008, by Object Refinery Limited.
*
*/
package demo;
import java.util.Arrays;
import java.util.Random;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.LogarithmicAxis;
import org.jfree.chart.axis.ProbabilityAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**
* A simple demonstration application showing how to create a probability axis
* line chart for a log-normal distribution using data from a random draw of a
* log-normal distribution.
*/
public class ProbabilityAxisDemoLog extends ApplicationFrame
{
/**
* Creates a new demo.
*
* @param title
* the frame title.
*/
public ProbabilityAxisDemoLog(String title)
{
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
/**
* Creates a sample dataset.
*
* @return a sample dataset.
*/
private static XYDataset createDataset()
{
XYSeries series1 = new XYSeries("Random Draw");
Random random = new Random(13);
double median = 10;
double logMedian = Math.log(median);
double sigma = 5;
double logSigma = Math.log(sigma);
double[] gaussian = new double[100];
// the percentage of the cumulative distribution
double[] x = new double[100];
for (int i = 0; i < gaussian.length; i++)
{
gaussian[i] = Math
.exp(random.nextGaussian() * logSigma + logMedian);
x[i] = i;
}
Arrays.sort(gaussian);
for (int i = 0; i < x.length; i++)
{
series1.add(x[i], gaussian[i]);
}
XYSeries series2 = new XYSeries(
"Log-Normal Distribution (median = 10, sigma = ln(5)");
series2.add(0.5, Math.exp(-2.575 * logSigma + logMedian));
series2.add(50.0, median);
series2.add(99.5, Math.exp(2.575 * logSigma + logMedian));
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series1);
dataset.addSeries(series2);
return dataset;
}
/**
* Creates a chart.
*
* @param dataset
* the data for the chart.
*
* @return a chart.
*/
private static JFreeChart createChart(XYDataset dataset)
{
// create the chart...
JFreeChart chart = ChartFactory.createXYLineChart(
"Probability Axis Log Demo", // chart title
"Cumulative Percent", // x axis label
"Y", // y axis label
dataset, // data
PlotOrientation.VERTICAL, true, // include legend
true, // tooltips
false // urls
);
// get a reference to the plot for further customization...
XYPlot plot = (XYPlot) chart.getPlot();
ProbabilityAxis xAxis = new ProbabilityAxis("Cumulative Percent");
plot.setDomainAxis(xAxis);
LogarithmicAxis yAxis = new LogarithmicAxis("Y");
plot.setRangeAxis(yAxis);
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesLinesVisible(0, false);
renderer.setSeriesShapesVisible(1, false);
renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
plot.setRenderer(renderer);
return chart;
}
/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* @return A panel.
*/
public static JPanel createDemoPanel()
{
JFreeChart chart = createChart(createDataset());
return new ChartPanel(chart);
}
/**
* Starting point for the demonstration application.
*
* @param args
* ignored.
*/
public static void main(String[] args)
{
ProbabilityAxisDemoLog demo = new ProbabilityAxisDemoLog(
"JFreeChart: ProbabilityAxisDemoLog.java");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
Enjoy,
John
-
stledger
- Posts: 14
- Joined: Sun Mar 09, 2008 7:39 am
Post
by stledger » Sun Mar 16, 2014 10:00 pm
Made some minor changes, basically taking our some magic numbers, and making it easier to change the number of random draws. This code is free, open, and submitted for use by the JFreeChart project.
John
Code: Select all
/* -------------------
* ProbabilityAxisDemoLog.java
* -------------------
* (C) Copyright 2004-2008, by Object Refinery Limited.
*
*/
package demo;
import java.util.Arrays;
import java.util.Random;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.LogarithmicAxis;
import org.jfree.chart.axis.ProbabilityAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**<p>
* A simple demonstration application showing how to create a probability axis
* line chart for a log-normal distribution using data from a random draw of a
* log-normal distribution.</p><p>
*
* @version 1.1 Feb 1, 2014
* Fixed a minor error in X axis values. Was plotting the lower side of the percent
* bin, rather than the middle of the bin. Also, now calculate the end point y axis values,
* rather than just pulling the number of standard deviations from a table.</p><p>
*/
public class ProbabilityAxisDemoLog extends ApplicationFrame
{
/**
* Creates a new demo.
* @param title
* the frame title.
*/
public ProbabilityAxisDemoLog(String title)
{
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
/**
* Creates a sample dataset.
*
* @return a sample dataset.
*/
private static XYDataset createDataset()
{
XYSeries series1 = new XYSeries("Random Draw");
Random random = new Random(13);
double median = 10;
double logMedian = Math.log(median);
double sigma = 5;
double logSigma = Math.log(sigma);
int n = 500; // number of random draws
double[] gaussian = new double[n];
// the percentage of the cumulative distribution
double[] x = new double[n];
for (int i = 0; i < gaussian.length; i++)
{
gaussian[i] = Math
.exp(random.nextGaussian() * logSigma + logMedian);
x[i] = 100.0*(i/((double) n) + 1.0/(2.0*n));
}
Arrays.sort(gaussian);
for (int i = 0; i < x.length; i++)
{
series1.add(x[i], gaussian[i]);
}
// The distribution the random draws are made from
XYSeries series2 = new XYSeries(
"Log-Normal Distribution (median = 10, sigma = ln(5)");
series2.add(0.1, Math.exp(ProbabilityAxis.calculateInvCNF(0.1) * logSigma + logMedian));
series2.add(50.0, median);
series2.add(99.9, Math.exp(ProbabilityAxis.calculateInvCNF(99.9) * logSigma + logMedian));
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series1);
dataset.addSeries(series2);
return dataset;
}
/**
* Creates a chart.
*
* @param dataset
* the data for the chart.
*
* @return a chart.
*/
private static JFreeChart createChart(XYDataset dataset)
{
// create the chart...
JFreeChart chart = ChartFactory.createXYLineChart(
"Probability Axis Log Demo", // chart title
"Cumulative Percent", // x axis label
"Y", // y axis label
dataset, // data
PlotOrientation.VERTICAL, true, // include legend
true, // tooltips
false // urls
);
// get a reference to the plot for further customization...
XYPlot plot = (XYPlot) chart.getPlot();
ProbabilityAxis xAxis = new ProbabilityAxis("Cumulative Percent");
plot.setDomainAxis(xAxis);
LogarithmicAxis yAxis = new LogarithmicAxis("Y");
plot.setRangeAxis(yAxis);
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesLinesVisible(0, false);
renderer.setSeriesShapesVisible(1, false);
renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
plot.setRenderer(renderer);
return chart;
}
/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* @return A panel.
*/
public static JPanel createDemoPanel()
{
JFreeChart chart = createChart(createDataset());
return new ChartPanel(chart);
}
/**
* Starting point for the demonstration application.
*
* @param args
* ignored.
*/
public static void main(String[] args)
{
ProbabilityAxisDemoLog demo = new ProbabilityAxisDemoLog(
"JFreeChart: ProbabilityAxisDemoLog.java");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}