0
votes

I wrote a perl script to parse some files and get some data. Now I wanna use gnuplot to generate charts for these data. Can I pass the variables from perl to gnuplot?

By the way, since I don't have Chart::Graph in the system, I'm planning to use pipe, something like this

open GP, '| gnuplot';.

1

1 Answers

3
votes
use strict;
use warnings;
use 5.014;


open my $PROGRAM, '|-', 'gnuplot'
    or die "Couldn't pipe to gnuplot: $!";

say {$PROGRAM} 'set terminal postscript';
say {$PROGRAM} "set output 'plot.ps'";
say {$PROGRAM} "plot 'mydata.dat' using 1:2 title 'Column'";

close $PROGRAM;

The command:

set terminal postscript

sets up gnuplot to produce postscript output. To see the list of possible output formats type:

gnuplot> set terminal

The command:

set output 'plot.ps'

directs the output to the file plot.ps.

The command:

plot 'mydata.dat' using 1:2 title 'Column'

reads some data from the file mydata.dat and plots it.

To enter data on the command line, you specify "-" as the filename and use $ variables:

gnuplot> plot "-" using ($1):($2) with lines title 'My Line'
input data ('e' ends) > 1 2
input data ('e' ends) > 3 4
input data ('e' ends) > e
gnuplot> 

So you can alter the perl program like this:

use strict;
use warnings;
use 5.014;

open my $PROGRAM, '|-', 'gnuplot'
    or die "Couldn't pipe to gnuplot: $!";

say {$PROGRAM} 'set terminal postscript';
say {$PROGRAM} "set output 'plot.ps'";
say {$PROGRAM} "plot '-' using (\$1):(\$2) with lines title 'My Line'";
print {$PROGRAM} "1 2\n3 4\ne\n";

close $PROGRAM;

To plot circles, you can do this:

gnuplot> set xrange [-2:5]    
gnuplot> set yrange[-2:5]     
gnuplot> plot "-" using ($1):($2):($3) with circles title 'My Circles'
input data ('e' ends) > 0 0 1     ****(x,y,radius)
input data ('e' ends) > 1 1 2
input data ('e' ends) > e
gnuplot>