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

gnuplot output to tkcanvas

by tallulah (Novice)
on Nov 20, 2009 at 08:12 UTC ( #808358=perlquestion: print w/replies, xml ) Need Help??
tallulah has asked for the wisdom of the Perl Monks concerning the following question:

i am running winxp with perl 10, and have tried to run gnuplot to output the plot to the tkcanvas , i have used zentara example here to produce a nice picture, the code is listed below,
just 3 problems, in the first run of the program i have an error message :""Use of uninitialized value in subroutine entry at "" ie about the line "$gnuplot->($canvas);"
in the second run the program run without problems; it seems it is running the previous created
second problem: is that once exited the gnuplot still running in memory; how to close it and all the processes.
the third problem: it seems that the tkcanvas can't display 3D plots such as plotting "splot sin(x*y/20)\n"
#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::ROText; use IPC::Open3; $|++; my $mw = MainWindow->new; my $canvas = $mw->Canvas( -bg => 'white', -height =>400, -width =>500, )->pack(); my $button = $mw -> Button(-text => "Quit", -command => \&quit)->pack(-side=>'left'); my $text = $mw->ROText(-bg=>'white') ->pack( -fill => 'both', -expand => 1 ); $text->tagConfigure( 'red', -foreground => 'red' ); my $pid = open3( \*IN, \*OUT, \*ERR, "c:/gnuplot/bin/gnuplot.exe" ) || + die; $mw->fileevent( \*OUT, readable => \&read_out ); $mw->fileevent( \*ERR, readable => \&read_err ); print IN "set term tkcanvas perltk interactive\n"; print IN "set output \'\'\n"; #print IN "a=1\n"; #print IN "plot a\n"; #print IN "plot sin(x)/x\n"; #print IN "splot sin(x*y/20)\n"; print IN "plot sin(x) title 'Sine Function', tan(x) title 'Tangent'\n" +; my $gnuplot = do ""; $gnuplot->($canvas); MainLoop; sub read_out { } sub read_err { print "read_err()\n"; my $num = sysread(ERR, my $buffer, 1024 ); $text->insert( 'end', $buffer, 'red' ); } sub quit{close $pid; exit}
on the other hand in the same zentara page there is a second solution to plot the graphic directly to the canvas, i was not able to run the code ,just the canvas without plot, the same is the superformula example i got an error message (open3: Can't locate auto/Tk/ROText/ in @INC (@INC contains: c:/perl/sit e/lib c:/perl/lib .) at c:/perl/site/lib/Tk/ line 469 ) related to the presence of "tie(*STDOUT, 'Tk::Text', $text);" i wish zentara or someone to make those examples available for windows systems, thanks
at last i have found a nice example in the book "Gnuplot in Action" to run gnuplot from perl: it is directing the output to graph.png file
open PROC, "| c:/gnuplot/bin/gnuplot.exe" || die "Could not start gnup +lot: $!"; #above :the first character in the filename is a pipe symbol (|) print PROC "set xlabel 'Time'; set ylabel 'Amplitude';"; print PROC "set o 'graph.png';"; print PROC "set t png;"; print PROC "plot '-' u 1:2 t 'data' w l\n"; for( $x = 0.0; $x <= 10.0; $x += 0.1 ) { $y = exp(-$x/5)*sin( $x ); print PROC "$x $y\n"; } print PROC "e\n"; close PROC;
thanks for your patience

Replies are listed 'Best First'.
Re: gnuplot output to tkcanvas
by zentara (Archbishop) on Nov 20, 2009 at 13:46 UTC
    i wish zentara or someone to make those examples available for windows systems, thanks

    ... since Microsoft expects me to pay for their inferior software, i would expect to be paid to write software for it..... so it appears that you tallulah have been chosen to solve the problem on win32.. good luck...

    ....i wish tallulah would get a decent OS, like Ubuntu linux.. :-) might see if SuperFormula with gnuplot and Tk runs for you, or read the gnuplot docs very carefully about the details on handling the win32 output types

    .... getting 3-d stuff to run is another layer of complexity , that isn't directly Perl involves getting your system setup properly.... but 3d is generally unsatisfactory on most computers out there....except the fast ones.... so you won't get much help with 3 d..... google for other 3d options, like matlab, scilab and the other scientific visualization tools for widows

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: gnuplot output to tkcanvas
by Anonymous Monk on Nov 21, 2009 at 09:29 UTC
      thank you very much, cool examples; it is working well on my pc
      a great addition to the topic
        the author of the earthquakes article said "this will only work on *nix style systems with gnuplot installed." but i can run it on the windows xp perl 5.10 gnuplot 4.5 beta , only i can't exit by clicking 'x' but can exit by ctrl-c.
        the second example works well.
Re: gnuplot output to tkcanvas
by Anonymous Monk on Nov 21, 2009 at 09:42 UTC
    Its some kind of timing issue, you'll have to sleep before you do "plot.z";

    You'll also have to

    print IN "\nexit\n";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://808358]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-04-20 13:34 GMT
Find Nodes?
    Voting Booth?