我需要自己制作一些散点图,所以我使用了其他答案中建议的模块。就我的口味而言,GD::Graph::Cartesian生成的数据点太大了,并且该模块没有提供控制此参数的方法,所以我破解了我的副本(如果你想这样做,请Cartesian.pm
搜索)。iconsize
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;