GD :: Graph :: histogram无法正常工作

时间:2012-06-21 10:40:58

标签: perl graph

我正在尝试使用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", ".";

运行它会在调用绘图函数时生成错误并生成错误的直方图:

Use of uninitialized value in string eq at /usr/lib/perl5/vendor_perl/5.12.4/GD/Graph/histogram.pm line 42.

显然,我将数据传递给函数的方式有问题。我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:2)

GD :: Graph :: Histogram中的第42行是指参数histogram_type,它应该默认为Count,但看起来默认不起作用。

所以尽量包括

histogram_type => 'count',