Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Interface to Gnuplot ?

by jflevi (Beadle)
on May 10, 2008 at 02:30 UTC ( [id://685819]=perlquestion: print w/replies, xml ) Need Help??

jflevi has asked for the wisdom of the Perl Monks concerning the following question:

Good evening, Good Monks, Looking for a Perl interface to gnuplot, I found "Graphics::GnuPlotIF" on CPAN. Before I spend time learning this module, since there was no reviews, I am asking this community for feedback on this module as well as suggestions; I am plotting simple trig functions.

Thanks for your time !!!

Life is tough, it's tougher when you are dumb...

jfl

Replies are listed 'Best First'.
Re: Interface to Gnuplot ?
by educated_foo (Vicar) on May 10, 2008 at 02:47 UTC
    You're best of just doing
    open GP, '| gnuplot'; print GP "...";
    because GNUPlog language isn't that hard to learn, and it has enough options that a Perl binding probably won't do what you want. Also, GNUPlot has excellent documentation via the 'help' command.
Re: Interface to Gnuplot ?
by zentara (Archbishop) on May 10, 2008 at 13:02 UTC
    This ought to get you started, read the gnuplot docs for the commands which gnuplot will accept interactively.
    #!/usr/bin/perl -w use strict; my $pid; &start_gnuplot; sub start_gnuplot{ $pid = open(GP, "| '/usr/bin/gnuplot' 2>&1 "); syswrite(GP, "plot sin(x)\n"); sleep 2; syswrite(GP, "plot tan(x)\n"); sleep 2; syswrite(GP, "quit\n"); }
    and here is the same thing with a simple Tk frontend
    #!/usr/bin/perl use warnings; use strict; # echo "plot sin (x)" | gnuplot -persist use Tk; use Tk::LabEntry; use FileHandle; my $gnuplot_path = "/usr/bin/gnuplot"; my $top = new MainWindow; my $function = "sin(x)"; my $funcentry = $top->LabEntry( -label => 'Function:', -textvariable => \$function, -labelPack => [ -side => 'left' ] )->pack; my $butframe = $top->Frame->pack( -anchor => 'w' ); my $plotbutton = $butframe->Button( -text => 'Plot', -command => \&plot, )->pack( -side => 'left' ); $butframe->Button( -text => 'Quit', -command => \&quit, )->pack( -side => 'left' ); $top->protocol( 'WM_DELETE_WINDOW', \&quit ); my $gnuplot = new FileHandle( "| $gnuplot_path" ); $gnuplot->autoflush( 1 ); $top->bind( "<Return>", sub { $plotbutton->invoke } ); MainLoop; sub plot { $gnuplot->print( "plot $function\n" ) if $function ne ''; } sub quit { $gnuplot->close; $top->destroy; }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Interface to Gnuplot ?
by rg0now (Chaplain) on May 10, 2008 at 20:40 UTC
    Kinda funny that the Perl interface to gnuplot, Chart::Graph was not mentioned by anyone. I have used it quite a lot and it is a clean and lean way to produce presentation-quality charts. It also comes with excellent documentation.
    So go ahead, install it and have some charting fun!
Re: Interface to Gnuplot ?
by ambrus (Abbot) on May 10, 2008 at 12:46 UTC

    Just write data to file and then pipe commands to gnuplot. Alternately, use octave's plotting interface which uses gnuplot – though you can't access all functions of gnuplot easily this way, it's often easier.

    Some other related nodes are Re^3: measuring IN/OUT traffic on your computer which shows an extract from a program I wrote that uses gnuplot to plot exactly this way; Re: Lapack, inline and Perl which shows a complete example to transfer numerical data to octave, you just have to change the octave commands to include a plot; and the thread Easy plotting ?.

    Update 2008-01-29: see Plot a spiral with gnuplot.

Re: Interface to Gnuplot ?
by Khen1950fx (Canon) on May 11, 2008 at 08:24 UTC
    Just to satisfy my curiosity, I used CPAN to download Graphics::GnuplotIF and Chart::Graph::Gnuplot. I also ran runprove on both of them. Chart::Graph failed tests, but Graphics::GnuplotIF passed all tests when downloaded from CPAN and also passed most tests with runprove. I'd go with Graphics::GnuplotIF.

    Update: Here's an example taken straight from the docs:

    #!/usr/bin/perl use strict; use warnings; use Graphics::GnuplotIF; my $plot = Graphics::GnuplotIF->new; $plot->gnuplot_set_xrange( 0, 4 ); $plot->gnuplot_set_yrange( -2, 2 ); $plot->gnuplot_cmd( "set grid" ); $plot->gnuplot_plot_equation( "y1(x) = sin(x)", "y2(x) = cos(x)", "y3(x) = sin(x)/x" ); $plot->gnuplot_pause();

      i am getting this error on running the program -- "Graphics::GnuplotIF <object 1>: problem closing communication to gnuplot" --

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://685819]
Approved by kyle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-24 17:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found