我正在尝试使用GD::Graph::histogram,根据存储在哈希中的一些数据创建直方图。我的代码如下所示:
sub __gen_pval_histogram {
my ($tbl_ref, $outfile, $outdir) = @_;
my $data = [values %$tbl_ref];
foreach (@$data) {
$_ = int( -1 * log($_)/log(10) );
}
my $hist = new GD::Graph::histogram(400,600);
$hist->set (x_label => "p-value",
y_label => "count",
title => "$outfile",
x_labels_vertical => 1,
bar_spacing => 0,
shadow_depth => 1,
shadowclr => 'dred',
transparent => 0,
) or warn $hist->error;
my $out = $hist->plot($data) or die $hist->error;
open my $file, ">", "$outdir/$outfile" or
die "Couldn't open $outdir/$outfile: '$?'";
binmode $file;
print {$file} $out->png;
print "Created histogram at $outdir/$outfile\n";
}
my %hash = (a => 0.0000009, b => 0.000034, c => 0.00045, d => 0.0000000012, e => 0.00000098);
__gen_pval_histogram \%hash, "hist.png", ".";
运行此命令会在调用 plot 函数时生成错误并生成错误的直方图:
Use of uninitialized value in string eq at /usr/lib/perl5/vendor_perl/5.12.4/GD/Graph/histogram.pm line 42.
显然,我将数据传递给函数的方式有问题。我应该怎么做才能解决这个问题?