Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Easy plotting ?

by jflevi (Beadle)
on Feb 16, 2008 at 23:11 UTC ( #668364=perlquestion: print w/ replies, xml ) Need Help??
jflevi has asked for the wisdom of the Perl Monks concerning the following question:

Hi Good Monks,

Pretty new to Perl!

I have written a program that calculates series of value pairs and I am looking for a simple way to create a rough cartesian plot using the first value of the pair as "x" and the second as "y" coordinates.
It does not need to be pretty or to be printed.

After a search on CPAN and here I am totally confused.

Running Perl 5.8.8 under Ubuntu 7.10.

Thanks for your help.

Just another Perl Beginner !

Comment on Easy plotting ?
Re: Easy plotting ?
by GrandFather (Cardinal) on Feb 16, 2008 at 23:20 UTC

    You may find Chart does what you want.


    Perl is environmentally friendly - it saves trees
Re: Easy plotting ?
by Joost (Canon) on Feb 16, 2008 at 23:50 UTC
Re: Easy plotting ?
by educated_foo (Vicar) on Feb 17, 2008 at 03:00 UTC
    If you don't want to use Excel or its Linux equivalent ("gexcel"? "kexcel"?), it behooves you to learn Gnuplot, write the data as columns to a file, then
    plot 'FILENAME'
    Gnuplot is extensively documented via its 'help' command, which contains many useful examples.
Re: Easy plotting ?
by ogr (Initiate) on Feb 17, 2008 at 06:17 UTC
    Have you seen GnuPlot? I'm planning to do something similar by integrating gnuplot with perl, but I'm just getting started myself. http://www.gnuplot.info/
Re: Easy plotting ?
by zentara (Archbishop) on Feb 17, 2008 at 17:52 UTC
    Easy plotting comes with Tk. Run the "widget" demo, and look under the Canvas section for "Simple 2-d Plot". Setting up the graph hash marks can be a challenge. See Tk Realtime data aquisition and Tk::Graph Here is a super simple plot on a Tk canvas. I plot values of sin, but you can substitute your discrete values. Another few examples follow below.
    #!/usr/bin/perl -w use strict; use Tk; my($o, $s) = (250, 20); my($pi, $x, $y) = (3.1415926, 0); my $mw = MainWindow->new; my $c = $mw->Canvas(-width => 500, -height => 500); $c->pack; $c->createLine(50, 250, 450, 250); $c->createText(10, 250, -fill => 'blue', -text => 'X'); $c->createLine(250, 50, 250, 450); $c->createText(250, 10, -fill => 'blue', -text => 'Y'); for ($x = -(3*$pi); $x <= +(3*$pi); $x += 0.1) { $y = sin($x); $c->createText( $x*$s+$o, $y*$s+$o, -fill => 'red', -text => ':'); $y = cos($x); $c->createText( $x*$s+$o, $y*$s+$o, -fill => 'green', -text => '|' +); } MainLoop;
    If you want something more professional, try gnuplot. It can be run thru Tk too.
    #!/usr/bin/perl use Tk; use Tk::LabEntry; use FileHandle; use strict; # echo "plot sin (x)" | gnuplot -persist 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; }
    And here is a neat little snippet I have( I didn't write it ), that uses Tk::Graph.
    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Graph; my $Main = MainWindow->new( -title => "Test", -background => "white" ) +; $Main->geometry( '+100+100' ); my $pollsec = 3; my $f1 = $Main->Frame()->pack( -side => "left", -fill => "both", -expa +nd => 1 ); my $g1 = $f1->Graph( -type => "Line", -legend => 0, -headroom => 0, -foreground => "black", -debug => 0, -borderwidth => 2, -titlecolor => '#435d8d', -yformat => '%g', -ylabel => "Mb", -xformat => "%g", -xlabel => "Requests", -barwidth => 15, -padding => [ 50, 20, -30, 50 ], # Padding [top, right, butt +om, left] -printvalue => '%s', # Name: Wert -linewidth => 2, -shadow => '#435d8d', -shadowdepth => 3, -dots => 1, -look => 20, -wire => "#d2e8e4", -max => 1024, -ytick => 8, -xtick => 5, -config => { Used => { -color => "#2db82a" } }, )->pack( -side => "bottom", -expand => 1, -fill => 'both', ); update_system_memory( $g1, $pollsec * 1000 ); MainLoop; ##################################################### sub update_system_memory { my $wid = shift or return undef; my $ms = shift or return undef; my $data = {}; my $total = $data->{ Total } = 1024; my $free = $data->{ Free } = int rand( 1024 ); my $used = $total - $free; my $percent = 100 * $used / $total; my $title = sprintf "\nSystem Memory\nMax: %d %s\nUsed: %d %s", calc_unit_from_kb( $total * 1024, "Mb" ), calc_unit_from_kb( $used * 1024, "MB" ); $wid->configure( -title => $title, -config => { Used => { -color => ( $percent >= 50 ? ( $percent >= 90 ? "#ff3333" : "#ffb200" ) : "#2db82a" ) } }, ); printf "used=%s\n", $used; $wid->set( { Used => $used, } ); $wid->after( $ms, [ \&update_system_memory => $wid, $ms ] ); } sub calc_unit_from_kb { my $value = shift; # Uebergabe in Kb! return ( -0, "?" ) unless defined $value; my $unit = shift; $unit = uc $unit if defined $unit; my $u = ""; foreach ( qw/Kb Mb Gb Tb Pb/ ) { $u = $_; last if defined $unit and uc( $u ) eq $unit; last if $value < 1024 and not defined $unit; $value = $value / 1024; } return ( $value, $u ); }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Easy plotting ?
by andreas1234567 (Vicar) on Feb 17, 2008 at 19:48 UTC
    gnuplot file:
    $ cat benchmark.encryption.gnuplot set title "Time to insert 10000 records (Commit range=100) on db2 9.5. +0 on linux i386" set xlabel "Attempt" set ylabel "Seconds" set terminal png # gnuplot recommends setting terminal before ou +tput set output "benchmark.encryption.png" set datafile separator ";" plot [] [] "benchmark.encryption.data" using 2 title "multi-procs 2 ca +lls - no encryption" with lines lt 2, \ "benchmark.encryption.data" using 3 title "multi-procs 2 ca +lls - with encryption" with lines lt 1
    Data file:
    $ cat benchmark.encryption.data: "Iterations";"With encryption";"Without encryption" 10000;36,2022;34,9835 20000;37,2686;35,2299 30000;39,5372;35,7808 40000;47,0838;41,6646 50000;57,1038;49,4962 60000;64,371;61,3641 70000;73,308;65,7723 80000;81,6356;73,6454 90000;88,261;80,3874 100000;95,1036;86,5714
    Create plot (benchmark.encryption.png):
    gnuplot benchmark.encryption.gnuplot
    --
    Andreas
Re: Easy plotting ?
by jflevi (Beadle) on Feb 18, 2008 at 15:40 UTC
    Thanks to all for your replies; well, I guess I will get into learning about "gnuplot" ...

    Thanks for the help !!!

    JFL

Re: Easy plotting ?
by planetscape (Canon) on Feb 19, 2008 at 16:31 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (17)
As of 2014-07-22 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (109 votes), past polls