I need to make some scatter plots of my own, so I played around with the module suggested in the other answers. For my taste, the data points produced by GD::Graph::Cartesian are far too large, and the module provides no methods to control this parameter, so I hacked my copy of Cartesian.pm
(search for iconsize
if you want to do the same).
use strict;
use warnings;
use Text::CSV;
use GD::Graph::Cartesian;
# Parse CSV file and convert the data for the
# requested $type and $date into a list of [X,Y] pairs.
my ($csv_file, $type, $date) = @ARGV;
my @xy_points;
my %i = ( X => -1, Y => -1 );
open(my $csv_fh, '<', $csv_file) or die $!;
my $parser = Text::CSV->new();
$parser->column_names( $parser->getline($csv_fh) );
while ( defined( my $hr = $parser->getline_hr($csv_fh) ) ){
next unless $hr->{type} eq $type;
my $xy = $hr->{site};
$xy_points[++ $i{$xy}][$xy eq 'X' ? 0 : 1] = $hr->{$date};
}
# Make a graph.
my $graph = GD::Graph::Cartesian->new(
width => 400, # Image size (in pixels, not X-Y coordinates).
height => 400,
borderx => 20, # Margins (also pixels).
bordery => 20,
strings => [[ 20, 50, 'Graph title' ]],
lines => [
[ 0,0, 50,0 ], # Draw an X axis.
[ 0,0, 0,50], # Draw a Y axis.
],
points => \@xy_points, # The data.
);
open(my $png_file, '>', 'some_data.png') or die $!;
binmode $png_file;
print $png_file $graph->draw;