Skip to content

Plotting

Sambit Paul edited this page Dec 2, 2023 · 9 revisions

The library supports Line Plots and Scatter Plots.

Initialising a Plot

This step basically creates a LinePlot object and initialises an empty chart with the parameters provided.

Parameters (all parameters are optional)
  1. title: Title of the Plot
  2. x_axis: Title on the X-axis
  3. y_axis: Title on the Y-axis
  4. width: Width of the plot
  5. Height: Height of the plot
Code
Plotting fig = new Plotting(600, 500, "Sample Figure", "Time", "Signal");
fig.initialisePlot();

Adding Signals to the Plot

Parameters
  1. name: Name of the Signal used in the Legend
  2. x: Array to be plotted on the X-axis (optional)
  3. y: Array to be plotted on the Y-axis
  4. marker: Whether markers are to be plotted on the chart
Code
double[] signal1 = {2.0, 4.0, 2.0, 3.0, 1.0};
double[] signal2 = {3.4, 6.7, 2.2, 1.6, 3.6};
double[] time = {0.0, 1.0, 2.0, 3.0, 4.0};

fig.addSignal("Signal 1", time, signal1, true);
fig.addSignal("Signal 2", time, signal2, true);

Adding Points to the Plot

Parameters
  1. name: Name of the Signal used in the Legend
  2. x: Array to be plotted on the X-axis (optional)
  3. y: Array to be plotted on the Y-axis
  4. marker: Marker to be used. Can be 'x', 'o', '+', '#', '^'.
Code
final double[] points1 = {3.4, 6.7, 2.2, 1.6, 3.6};
final double[] points2 = {1.6, 2.0, 5.3, -1.3, 2.2};

fig.addPoints("Points 1", time, points1, 'x');
fig.addPoints("Points 2", time, points2);

Adding Horizontal and Vertical Lines to the Plot

Parameters

For Horizontal:

  1. x_min: Starting point of the line
  2. x_max: Stopping point of the line
  3. y: Position where the line is drawn on y-axis

For Vertical:

  1. x: Position where the line is drawn on x-axis
  2. y_min: Top point of the line
  3. y_max: Bottom point of the line
Code
// For vertical lines
double[][] verLines = {{0.0, 2.0, 3.4}, {1.0, 4.0, 6.7}, {2.0, 2.0, 2.2}, {3.0, 3.0, 1.6}, {4.0, 1.0, 3.6}};

for (int i=0; i<verLines.length; i++) {
    fig.vline(verLines[i][0], verLines[i][1], verLines[i][2]);
}

// For horizontal lines
fig.hline(0.0, 4.0, 4.0);
fig.hline(0.0, 4.0, 6.7);

Displaying and Saving the Plot

The image is saved as a PNG image.

Parameters
  1. name: Filename to be saved as (extension is required)
Code
String outputFileName = "signal.png";
// To plot on a window
fig.plot();
// TO save as an image
fig.saveAsPNG(outputFileName);

OUTPUT:

plotting

Clone this wiki locally