开发者

perl plotting using GD:Graph

开发者 https://www.devze.com 2023-01-08 12:05 出处:网络
I have data like this. file1 date1176.09 date10 10 87.09 date11 11 89.89 date2266.5 date3378.89 date4487.98

I have data like this.

file1 date1  1  76.09
      date10 10 87.09
      date11 11 89.89
      date2  2  66.5
      date3  3  78.89
      date4  4  87.98
      date5  5  57
      date6  6  57.98
      date7  7  34.32
      date8  8  45.76
      date9  9  34.99
file2 date1  1  82.45
      date2  2  86.23
file3 date1  1  65.76
      date10 10 34.89
      date11 11 23.79
      date12 12 86.23
      date2  2  23.78
      date3  3  45.78
      date4  4  34.78
      date5  5  67.89
      date6  6  34.78
      date7  7  78.45
      date8  8  67.89
      date9  9  86.23

I am storing the column 3 and column 4 values in array.

@x = [1,10,11,12,13,2,3,4,5,6,7,8,9]
and corresponding column 4 values in array @y

Now I plot the graph as folllows:

my @data = ([@x], [@y]);

my $mygraph = GD::Graph::lines->new(500, 300);
$mygraph->set(
                        x_label     => 'X axis',
                        y_label     => 'Y axis',
                        title       => "stats",
                    ) or warn $mygraph->error;

 my $myimage = $mygraph->plot(\@data) or die $mygraph->error;

My problem is...

In开发者_如何学JAVA the graph the X-axis has values displayed as 1,10,11,12,13,2,3,4,5,6,7,8,9 and corresponding y values plotted. I cannot hard code the x -axis values as I extract these data from a file into these arrays. How do I make sure that X-axis has values 1,2,3,4,5,6,7,8,9,10,11,12,13 and has corresponding y values plotted.

Also I have abother file which has data like:

file1 date1  1  1
      date10 10 2
      date11 11 2
      date2  2  2
      date3  3  3
      date4  4  3
      date5  5  3
      date6  6  4
      date7  7  4
      date8  8  4
      date9  9  4
file2 date1  1  3
      date2  2  2
file3 date1  1  2
      date10 10 3
      date11 11 3
      date12 12 3
      date2  2  3
      date3  3  1
      date4  4  2
      date5  5  5
      date6  6  1
      date7  7  1
      date8  8  2
      date9  9  2

I can plot graohs for datasets in both of these files separately. But what I want to do is, I want to show a single graph with column 3 and column 4 of first file plotted along with the column 3 and column 4 of 2nd file plotted in single graph.

Can somebody please help me with these two issues.

Thank you.


Here's another try. I won't use GD::Graph, as I've already began this answer in your other post with Graphics::GnuplotIF.

Your two problems, as I understand them, are:

  1. You want to numerically sort the x-values.
  2. You want to plot multiple series.

Right:

use Graphics::GnuplotIF;

my $last;
my %data_per_file;

LOAD:
while (<>) {
    chomp;
    my ($file, $label, $x, $y) = split /\s+/;
    $last = $file if $file;
    push @{$data_per_file{$last}}, [$label, $x, $y];
}

SORT:
for my $file (keys %data_per_file) {
    @{$data_per_file{$file}}
    = sort { $a->[1] <=> $b->[1] }
      @{$data_per_file{$file}};
}

my @all_data_in_pairs;

PLOT_SINGLE:
for my $file (keys %data_per_file) {
    my @labels = map { $_->[0] } @{$data_per_file{$file}};
    my @x      = map { $_->[1] } @{$data_per_file{$file}};
    my @y      = map { $_->[2] } @{$data_per_file{$file}};

    my $plot = Graphics::GnuplotIF->new(persist => 1, style => 'points');
    $plot->gnuplot_set_title( $file );
    $plot->gnuplot_plot_xy( \@x, \@y );

    push @all_data_in_pairs, \@x, \@y;
}

my $plot = Graphics::GnuplotIF->new(persist => 1, style => 'lines');
$plot->gnuplot_set_xrange(0, 10);
$plot->gnuplot_hardcopy( 'output.ps',
                         'postscript',
                         'color lw 3' )
if $want_hardcopy;

$plot->gnuplot_set_title( 'all' );
$plot->gnuplot_plot_many( @all_data_in_pairs );

$plot->gnuplot_restore_terminal();


Post merged. Updated version at perl plotting using GD:Graph.

Perl doesn't have 'native' plotting facilities: you'll have to use an external library, so you might as well use gnuplot if available. There are plenty of interfaces to gnuplot on CPAN. One that I've used and seems to work well is Graphics::GnuplotIF.

You could do something like the following:

use Graphics::GnuplotIF;

my $last;
my %data_per_file;

LOAD:
while (<>) {
    chomp;
    my ($file, $label, $x, $y) = split /\s+/;
    $last = $file if $file;
    push @{$data_per_file{$last}}, [$label, $x, $y];
}

PLOT:
for my $file (keys %data_per_file) {
    my @labels = map { $_->[0] } @{$data_per_file{$file}};
    my @x      = map { $_->[1] } @{$data_per_file{$file}};
    my @y      = map { $_->[2] } @{$data_per_file{$file}};

    my $plot = Graphics::GnuplotIF->new(persist => 1, style => 'points');
    $plot->gnuplot_set_title( $file );
    $plot->gnuplot_plot_xy( \@x, \@y );
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号