Class Plot

java.lang.Object
org.moeaframework.analysis.plot.Plot

public class Plot extends Object
Provides simple 2D plotting capabilities. This is intended to allow the rapid creation of 2D plots, supporting:
  1. scatter plots of bi-objective populations,
  2. line plots of runtime dynamics (via an Observations,
  3. box-and-whisker plots of performance statistics (via an Analyzer), and
  4. other plots of basic data types (e.g., line, scatter, area, stacked, heat map).
It is possible to combine datasets by calling more than one add method, but you can not mix different plot types (e.g., XY plots versus categorical plots). Thus, box-and-whisker plots can not be overlaid on a line plot.

In general, one should first generate the plot artifacts by calling line(java.lang.String, double[], double[]), scatter(java.lang.String, double[], double[]), area(java.lang.String, double[], double[]), stacked(java.lang.String, double[], double[]), heatMap(java.lang.String, double[], double[], double[][]) or any of the add(java.lang.String, org.moeaframework.core.Population) methods. Artifacts can be customized by immediately calling one of the with* methods after generating the artifact (the customization is only applied to the last artifact). Lastly, call the set* methods to customize the overall appearance of the chart. Call show() to display the chart in a window or save(java.lang.String) to create an image file. For example:

       new Plot()
                .scatter("Point", new double[] { 0.0, 1.0, 2.0 }, new double[] { 3.0, 4.0, 5.0 })
                        .withPaint(Color.BLACK)
                .line("Line", new double[] { 0.0, 2.0 }, new double[] { 3.0, 5.0 })
                        .withSize(5)
                .setXLabel("X")
                .setYLabel("Y")
                .setTitle("Example")
                .show();
 

This class is not intended to be a fully featured plotting library. To generate more sophisticated plots or customize their appearance, one must instead use JFreeChart, JZY3D, or another plotting library.

Generated plots can be saved to PNG or JPEG files. If JFreeSVG is available on the classpath, SVG files can be generated. JFreeSVG can be obtained from http://www.jfree.org/jfreesvg/.

  • Constructor Details

    • Plot

      public Plot()
      Creates a new, empty plot.
  • Method Details

    • setTitle

      public Plot setTitle(String title)
      Sets the chart title.
      Parameters:
      title - the title
      Returns:
      a reference to this Plot instance
    • setXLabel

      public Plot setXLabel(String label)
      Sets the x-axis label.
      Parameters:
      label - the label for the x-axis
      Returns:
      a reference to this Plot instance
    • setYLabel

      public Plot setYLabel(String label)
      Sets the y-axis label.
      Parameters:
      label - the label for the y-axis
      Returns:
      a reference to this Plot instance
    • setBackgroundPaint

      public Plot setBackgroundPaint(Paint paint)
      Sets the background paint.
      Parameters:
      paint - the background paint
      Returns:
      a reference to this Plot instance
    • setGridPaint

      public Plot setGridPaint(Paint paint)
      Sets the grid line paint.
      Parameters:
      paint - the grid line paint
      Returns:
      a reference to this Plot instance
    • setXLim

      public Plot setXLim(double min, double max)
      Sets the x-axis limits.
      Parameters:
      min - the minimum bound for the x-axis
      max - the maximum bound for the x-axis
      Returns:
      a reference to this Plot instance
    • setYLim

      public Plot setYLim(double min, double max)
      Sets the y-axis limits.
      Parameters:
      min - the minimum bound for the y-axis
      max - the maximum bound for the y-axis
      Returns:
      a reference to this Plot instance
    • scatter

      public Plot scatter(String label, double[] x, double[] y)
      Creates a new scatter plot series.
      Parameters:
      label - the label for the series
      x - the x values
      y - the y values
      Returns:
      a reference to this Plot instance
    • scatter

      public Plot scatter(String label, List<? extends Number> x, List<? extends Number> y)
      Creates a new scatter plot series.
      Parameters:
      label - the label for the series
      x - the x values
      y - the y values
      Returns:
      a reference to this Plot instance
    • add

      public Plot add(String label, Population population)
      Displays the solutions in the given population in a 2D scatter plot. Only two objectives will be displayed.
      Parameters:
      label - the label for the series
      population - the population
      Returns:
      a reference to this Plot instance
    • add

      public Plot add(String label, Population population, int x, int y)
      Displays the solutions in the given population in a 2D scatter plot. The two given objectives will be displayed.
      Parameters:
      label - the label for the series
      population - the population
      x - the objective to plot on the x-axis
      y - the objective to plot on the y-axis
      Returns:
      a reference to this Plot instance
    • add

      public Plot add(Observations observations)
      Displays the runtime data stored in an Observations as one or more line plots.
      Parameters:
      observations - the Observations instance
      Returns:
      a reference to this Plot instance
    • add

      public Plot add(String label, Observations observations, String metric)
      Displays the runtime data for the given metric as a line plot.
      Parameters:
      label - the label for the series
      observations - the Observations instance
      metric - the name of the performance metric to plot
      Returns:
      a reference to this Plot instance
    • line

      public Plot line(String label, double[] x, double[] y)
      Creates a new line plot series.
      Parameters:
      label - the label for the series
      x - the x values
      y - the y values
      Returns:
      a reference to this Plot instance
    • line

      public Plot line(String label, List<? extends Number> x, List<? extends Number> y)
      Creates a new line plot series.
      Parameters:
      label - the label for the series
      x - the x values
      y - the y values
      Returns:
      a reference to this Plot instance
    • area

      public Plot area(String label, double[] x, double[] y)
      Creates a new area plot series.
      Parameters:
      label - the label for the series
      x - the x values
      y - the y values
      Returns:
      a reference to this Plot instance
    • area

      public Plot area(String label, List<? extends Number> x, List<? extends Number> y)
      Creates a new area plot series.
      Parameters:
      label - the label for the series
      x - the x values
      y - the y values
      Returns:
      a reference to this Plot instance
    • stacked

      public Plot stacked(String label, double[] x, double[] y)
      Creates a new stacked area plot series. The data will be stacked with any preceding calls to stacked.
      Parameters:
      label - the label for the series
      x - the x values
      y - the y values
      Returns:
      a reference to this Plot instance
    • stacked

      public Plot stacked(String label, List<? extends Number> x, List<? extends Number> y)
      Creates a new stacked area plot series. The data will be stacked with any preceding calls to stacked.
      Parameters:
      label - the label for the series
      x - the x values
      y - the y values
      Returns:
      a reference to this Plot instance
    • heatMap

      public Plot heatMap(String label, double[] x, double[] y, double[][] z)
      Creates a new heat map series. The series is added to the given dataset, or if null a new dataset is created.
      Parameters:
      label - the label for the series
      x - the x values
      y - the y values
      z - the z values
      Returns:
      a reference to this Plot instance
    • heatMap

      public Plot heatMap(String label, List<? extends Number> x, List<? extends Number> y, List<? extends List<? extends Number>> z)
      Creates a new heat map series. The series is added to the given dataset, or if null a new dataset is created.
      Parameters:
      label - the label for the series
      x - the x values
      y - the y values
      z - the z values
      Returns:
      a reference to this Plot instance
    • add

      public Plot add(Analyzer analyzer)
      Displays the statistical results from an Analyzer as a box-and-whisker plot.
      Parameters:
      analyzer - the Analyzer instance
      Returns:
      a reference to this Plot instance
    • add

      public Plot add(Analyzer.AnalyzerResults result)
      Displays the statistical results from an Analyzer.AnalyzerResults as a box-and-whisker plot.
      Parameters:
      result - the AnalyzerResults instance
      Returns:
      a reference to this Plot instance
    • withSize

      public Plot withSize(float size)
      Modifies the line thickness or point size in the last dataset. The size is applied to all series in the dataset.
      Parameters:
      size - the size
      Returns:
      a reference to this Plot instance
    • withPaint

      public Plot withPaint(Paint... paint)
      Modifies the paint (e.g,. color) of each series in the last dataset. If the dataset contains more series than the number of arguments, the arguments are reused as needed.
      Parameters:
      paint - one or more paint instances
      Returns:
      a reference to this Plot instance
    • save

      public Plot save(String filename) throws IOException
      Saves the plot to an image file. The type of image is determined from the filename extension, which must match one of the supported file types in Plot.FileType.
      Parameters:
      filename - the filename
      Returns:
      a reference to this Plot instance
      Throws:
      IOException - if an I/O error occurred
    • save

      public Plot save(File file) throws IOException
      Saves the plot to an image file. The type of image is determined from the filename extension, which must match one of the supported file types in Plot.FileType.
      Parameters:
      file - the file
      Returns:
      a reference to this Plot instance
      Throws:
      IOException - if an I/O error occurred
    • save

      public Plot save(File file, String format, int width, int height) throws IOException
      Saves the plot to an image file. The format must match one of the supported file types in Plot.FileType.
      Parameters:
      file - the file
      format - the image format
      width - the image width
      height - the image height
      Returns:
      a reference to this Plot instance
      Throws:
      IOException - if an I/O error occurred
    • save

      public Plot save(File file, Plot.FileType fileType, int width, int height) throws IOException
      Saves the plot to an image file.
      Parameters:
      file - the file
      fileType - the image file format
      width - the image width
      height - the image height
      Returns:
      a reference to this Plot instance
      Throws:
      IOException - if an I/O error occurred
    • supportsSVG

      public static boolean supportsSVG()
      Returns true if saving to the SVG format is supported. This requires the JFreeSVG library to be setup on the classpath.
      Returns:
      true if saving to the SVG format is supported; false otherwise
    • getChart

      public org.jfree.chart.JFreeChart getChart()
      Returns the internal chart. Allows further modification of the appearance of the chart.
      Returns:
      the internal JFreeChart instance
    • getChartPanel

      public org.jfree.chart.ChartPanel getChartPanel()
      Returns the chart embedded in a Swing panel for display.
      Returns:
      a Swing panel for displaying the chart
    • show

      public JFrame show()
      Displays the chart in a standalone window.
      Returns:
      the window that was created
    • show

      public JFrame show(int width, int height)
      Displays the chart in a standalone window.
      Parameters:
      width - the width of the chart
      height - the height of the chart
      Returns:
      the window that was created
    • showDialog

      public JDialog showDialog()
      Displays the chart in a blocking JDialog.
      Returns:
      the window that was created
    • showDialog

      public JDialog showDialog(int width, int height)
      Displays the chart in a blocking JDialog.
      Parameters:
      width - the width of the chart
      height - the height of the chart
      Returns:
      the window that was created