Scatter Plot

Scatter Plot with best-fit line

Complete Code

Download file: ScatterPlot.pg

POD for Macro Files

PG problem file

Explanation

DOCUMENT();

loadMacros(
    'PGstandard.pl', 'PGML.pl',
    'plots.pl',      'PGstatisticsmacros.pl',
    'PGcourse.pl'
);

Preamble

This produces a scatter plot with plots.pl. A version of this with StatisticalPlots.pl is available in Scatter Plot (Statistical Plots).

@data = ([ 5, 40 ], [ 7, 120 ], [ 12, 180 ], [ 16, 210 ], [ 20, 240 ]);

$x = [ map { $_->[0] } @data ];
$y = [ map { $_->[1] } @data ];
($m, $b) = linear_regression($x, $y);

$plot = Plot(
    xmin        => 0,
    xmax        => 24,
    xtick_delta => 4,
    ymin        => 0,
    ymax        => 300,
    aria_label  => 'Linear Regression'
);
$plot->add_dataset(
    @data,
    linestyle => 'none',
    marks     => 'circle',
    color     => 'blue'
);
$plot->add_function(
    "$m x + $b",
    'x',
    1,
    23,
    color => 'black',
    width => 1,
);

Setup

For this problem, we have a data set stored as an array of array references. First we find the best-fit line using the linear_regression function of PGstatisticsmacros.pl. Note: the x and y data needs to be array references and the 2nd and 3rd lines extract the components.

For the plot, first set up the Plot with a plotting window that will capture the data. The add_dataset method adds the data to the plot with circles and color blue.

The line is added to the dataset with the add_function method. The 3rd and 4th arguments are the xmin and xmax of the domain.

BEGIN_PGML
A scatter plot of the data:

[``` [@ join(', ', map { "($_->[0], $_->[1])" }  @data)  @] ```]

together with the best-fit line is given by the following plot.

>> [! Scatter Plot with best fit line !]{$plot}{500} <<

END_PGML

Statement

To format the points in the @data array in a human-readable manner, these are first put in “(x, y)” form using the map function. The resulting array is then concatenated with the join command. Since both map and join are perl commands, this compound command is placed within a [@ @].

The [! !]{}{} is a PGML format for inserting an image.

BEGIN_PGML_SOLUTION
Solution explanation goes here.
END_PGML_SOLUTION

ENDDOCUMENT();

Solution

A solution should be provided here.