加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

使用perl画图

发布时间:2020-12-15 23:54:59 所属栏目:大数据 来源:网络整理
导读:zz :http://stackoverflow.com/questions/18150841/gdgraph-with-perl GD::Graph with Perl Assuming your data file is as follows,using tab delimiters. Student Name ScoreJack 89Jill 70Sandy 40 You could do something like this,pushing your? x ?a

zz :http://stackoverflow.com/questions/18150841/gdgraph-with-perl

GD::Graph with Perl


Assuming your data file is as follows,using tab delimiters.

Student Name         Score
Jack                  89
Jill                  70
Sandy                 40

You could do something like this,pushing your?x?axis and?y?axis values from your data file to arrays.

use strict;
use warnings;
use CGI qw( :standard );
use GD::Graph::bars;

open my $fh,'<','data.txt' or die $!;

my (@x,@y);
while (<$fh>) {
   next if $. == 1;            # skip header line
   push @x,(split /t/)[0];   # push 'Student Names' into @x array
   push @y,(split /t/)[1];   # push 'Score' into @y array
}
close $fh;

my $graph = GD::Graph::bars->new(800,800);

$graph->set( 
             x_label => 'Students',y_label => 'Scores',title   => 'Student Vs. Scores',) or warn $graph->error;

my @data = (@x,@y);
$graph->plot(@data) or die $graph->error();

print header(-type=>'image/jpeg'),$graph->gd->jpeg;

Giving you for example:?

enter image description here

If you are wanting to use multiple?y?axis values,assuming you have another tab delimiter column with for example?Score2,you could easily do something like this.

my (@x,@y,@y2);
while (<$fh>) {
   next if $. == 1;
   push @x,(split /t/)[0];
   push @y,(split /t/)[1];
   push @y2,(split /t/)[2];
}

And change your?@data?array to:

my @data = (@x,@y,@y2);

And your result would be:?

enter image description here

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读