Sending data from perl to gnuplot and getting an "ASCII-art" graph back?

Discussion in 'Perl Misc' started by Adam Funk, Jul 29, 2006.

  1. Adam Funk

    Adam Funk Guest

    I'm generating some data points in a Perl program and trying to send
    them to gnuplot and get back an "ASCII-art" graph (the kind that
    gnuplot generates with the "set term dumb" setting). I'd like to
    improve on the following steps:

    (1)
    foreach $x (sort {$a <=> $b} keys(%table) ) {
    $y = $table{$x};
    $line = sprintf("%5d %5d\n", $x, $y);
    $max_y = $y if ($y > $max_y);
    print($line) if ($option{v}); # verbose option
    push(@output,$line);
    }

    This produces lines like this:
    -5 2
    -2 5

    (2) Then I use recipe 7.5 from the Perl Cookbook to generate two temp
    files, $data_file and $cmd_file, and I write @output from step (1)
    into $data_file.

    (3) Then I write a bunch of gnuplot commands as lines to $cmd_file:
    set term dumb
    set ylabel \"Frequency\"
    set xlabel \"Time\"
    unset key
    set yrange [0:$max_y]
    plot \"$temp_filename\" with impulses

    (4) and call gnuplot thus:
    system('gnuplot', $cmd_file);

    The Perl program runs and prints the plot to the screen. I'm about to
    modify it to use backticks
    $gnuplot_output = `gnuplot $cmd_file`
    but I can't believe there isn't a better way than what I've done to
    send a list of commands to gnuplot and get the plot back.

    Is there?

    --
    Thanks,
    Adam
    Adam Funk, Jul 29, 2006
    #1
    1. Advertising

  2. Adam Funk

    Theo Hopman Guest

    Adam Funk wrote:
    > I'm generating some data points in a Perl program and trying to send
    > them to gnuplot and get back an "ASCII-art" graph (the kind that
    > gnuplot generates with the "set term dumb" setting). I'd like to
    > improve on the following steps:


    I don't use Perl, but can offer some general suggestions.

    > (1)
    > foreach $x (sort {$a <=> $b} keys(%table) ) {
    > $y = $table{$x};
    > $line = sprintf("%5d %5d\n", $x, $y);
    > $max_y = $y if ($y > $max_y);
    > print($line) if ($option{v}); # verbose option
    > push(@output,$line);
    > }
    >
    > This produces lines like this:
    > -5 2
    > -2 5


    Store this data in an array for later use.

    > (2) Then I use recipe 7.5 from the Perl Cookbook to generate two temp
    > files, $data_file and $cmd_file, and I write @output from step (1)
    > into $data_file.


    You should be able to avoid the use of temporary files entirely. See
    below.

    > (3) Then I write a bunch of gnuplot commands as lines to $cmd_file:
    > set term dumb
    > set ylabel \"Frequency\"
    > set xlabel \"Time\"
    > unset key
    > set yrange [0:$max_y]
    > plot \"$temp_filename\" with impulses


    What you really want to do is pipe these commands to the stdin of
    gnuplot. You also want to use inline data (see `help datafile
    special-filenames`) like such:

    plot "-" with impulses
    -5 2
    2 5
    e

    The data is supplied to gnuplot's stdin via the same pipe the commands
    go through.

    > (4) and call gnuplot thus:
    > system('gnuplot', $cmd_file);
    >
    > The Perl program runs and prints the plot to the screen. I'm about to
    > modify it to use backticks
    > $gnuplot_output = `gnuplot $cmd_file`
    > but I can't believe there isn't a better way than what I've done to
    > send a list of commands to gnuplot and get the plot back.


    I don't know how to do this in Perl, but in a shell script the
    following would work (plus or minus the fact that I haven't done this
    in a while):

    gnuplot_output=`echo $gnuplot-commands-and-data | gnuplot`

    THeo
    Theo Hopman, Jul 29, 2006
    #2
    1. Advertising

  3. In article <>,
    Adam Funk <> wrote:
    >I'm generating some data points in a Perl program and trying to send
    >them to gnuplot and get back an "ASCII-art" graph (the kind that
    >gnuplot generates with the "set term dumb" setting). I'd like to
    >improve on the following steps:
    >
    >(1)
    > foreach $x (sort {$a <=> $b} keys(%table) ) {
    > $y = $table{$x};
    > $line = sprintf("%5d %5d\n", $x, $y);
    > $max_y = $y if ($y > $max_y);
    > print($line) if ($option{v}); # verbose option
    > push(@output,$line);
    > }
    >
    >This produces lines like this:
    > -5 2
    > -2 5


    OK, although you could also just send this data directly to gnuplot.

    >
    >(2) Then I use recipe 7.5 from the Perl Cookbook to generate two temp
    >files, $data_file and $cmd_file, and I write @output from step (1)
    >into $data_file.


    >(3) Then I write a bunch of gnuplot commands as lines to $cmd_file:
    >set term dumb
    >set ylabel \"Frequency\"
    >set xlabel \"Time\"
    >unset key
    >set yrange [0:$max_y]
    >plot \"$temp_filename\" with impulses
    >
    >(4) and call gnuplot thus:
    > system('gnuplot', $cmd_file);


    You can do that. But you could also do
    it all in-line:

    $gnuplot = '/usr/local/bin/gnuplot';

    open(GNUPLOT, "| $gnuplot");
    print GNUPLOT <<EOFgnuplot;
    set term dumb
    set output "plot.txt"
    set ylabel "Frequency"
    set xlabel "Time"
    unset key
    plot '' with impulses
    EOFgnuplot

    foreach $x (sort {$a <=> $b} keys(%table) ) {
    ... the stuff you were doing anyhow ...
    printt GNUPLOT $line;
    }
    close(GNUPLOT);

    Then you can copy the output in "plot.txt" to the screen
    if you want, or dump it some other way.

    --
    Ethan A Merritt
    Ethan Merritt, Jul 30, 2006
    #3
  4. Adam Funk

    Ben Morrow Guest

    Quoth Adam Funk <>:
    > I'm generating some data points in a Perl program and trying to send
    > them to gnuplot and get back an "ASCII-art" graph (the kind that
    > gnuplot generates with the "set term dumb" setting). I'd like to
    > improve on the following steps:


    Did you try http://search.cpan.org/search?query=gnuplot ? It shows up
    two modules (Term::Gnuplot and Chart::Graph::Gnuplot) that look like
    they may help.

    Ben

    --
    The Earth is degenerating these days. Bribery and corruption abound.
    Children no longer mind their parents, every man wants to write a book,
    and it is evident that the end of the world is fast approaching.
    Assyrian stone tablet, c.2800 BC
    Ben Morrow, Jul 30, 2006
    #4
  5. Re: Sending data from perl to gnuplot and getting an "ASCII-art"graph back?

    Adam Funk wrote:
    > I'm generating some data points in a Perl program and trying to send
    > them to gnuplot and get back an "ASCII-art" graph (the kind that
    > gnuplot generates with the "set term dumb" setting). I'd like to
    > improve on the following steps:
    >
    > (1)
    > foreach $x (sort {$a <=> $b} keys(%table) ) {
    > $y = $table{$x};
    > $line = sprintf("%5d %5d\n", $x, $y);
    > $max_y = $y if ($y > $max_y);
    > print($line) if ($option{v}); # verbose option
    > push(@output,$line);
    > }
    >
    > This produces lines like this:
    > -5 2
    > -2 5
    >
    > (2) Then I use recipe 7.5 from the Perl Cookbook to generate two temp
    > files, $data_file and $cmd_file, and I write @output from step (1)
    > into $data_file.
    >
    > (3) Then I write a bunch of gnuplot commands as lines to $cmd_file:
    > set term dumb
    > set ylabel \"Frequency\"
    > set xlabel \"Time\"
    > unset key
    > set yrange [0:$max_y]
    > plot \"$temp_filename\" with impulses
    >
    > (4) and call gnuplot thus:
    > system('gnuplot', $cmd_file);
    >
    > The Perl program runs and prints the plot to the screen. I'm about to
    > modify it to use backticks
    > $gnuplot_output = `gnuplot $cmd_file`
    > but I can't believe there isn't a better way than what I've done to
    > send a list of commands to gnuplot and get the plot back.
    >
    > Is there?


    You can probably do what you want with IPC::Open2 or Expect.

    perldoc IPC::Open2
    perldoc Expect



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Jul 30, 2006
    #5
  6. Adam Funk

    Adam Funk Guest

    On 2006-07-29, Ethan Merritt <> wrote:

    > You can do that. But you could also do
    > it all in-line:
    >
    > $gnuplot = '/usr/local/bin/gnuplot';
    >
    > open(GNUPLOT, "| $gnuplot");
    > print GNUPLOT <<EOFgnuplot;
    > set term dumb
    > set output "plot.txt"
    > set ylabel "Frequency"
    > set xlabel "Time"
    > unset key
    > plot '' with impulses
    > EOFgnuplot
    >
    > foreach $x (sort {$a <=> $b} keys(%table) ) {
    > ... the stuff you were doing anyhow ...
    > printt GNUPLOT $line;
    > }
    > close(GNUPLOT);
    >
    > Then you can copy the output in "plot.txt" to the screen
    > if you want, or dump it some other way.


    That's a good idea, but I think I'll try Theo's suggestion first,
    since it should get gnuplot's output directly back into a variable in
    the Perl program without needing a temp file.

    Thanks.
    Adam Funk, Jul 30, 2006
    #6
  7. Adam Funk

    Adam Funk Guest

    On 2006-07-29, Theo Hopman <> wrote:

    > What you really want to do is pipe these commands to the stdin of
    > gnuplot.


    Exactly.

    > You also want to use inline data (see `help datafile
    > special-filenames`) like such:
    >
    > plot "-" with impulses
    > -5 2
    > 2 5
    > e


    I'd never come across this feature but it looks extremely useful...

    > I don't know how to do this in Perl, but in a shell script the
    > following would work (plus or minus the fact that I haven't done this
    > in a while):
    >
    > gnuplot_output=`echo $gnuplot-commands-and-data | gnuplot`


    ....for this (which is about the same in Perl). Thanks!
    Adam Funk, Jul 30, 2006
    #7
  8. Adam Funk

    Adam Funk Guest

    On 2006-07-29, Ben Morrow <> wrote:

    > Did you try http://search.cpan.org/search?query=gnuplot ?


    Obviously not!

    > It shows up two modules (Term::Gnuplot and Chart::Graph::Gnuplot)
    > that look like they may help.


    Interesting, but they look a bit heavy and oriented towards generating
    graphics files. (I'll try to keep them in mind if I need to do that
    later.)
    Adam Funk, Jul 30, 2006
    #8
  9. Adam Funk

    Adam Funk Guest

    On 2006-07-29, Theo Hopman <> wrote:

    > What you really want to do is pipe these commands to the stdin of
    > gnuplot. You also want to use inline data (see `help datafile
    > special-filenames`) like such:
    >
    > plot "-" with impulses
    > -5 2
    > 2 5
    > e

    ....
    > gnuplot_output=`echo $gnuplot-commands-and-data | gnuplot`


    The Perl equivalent of that works perfectly. Thanks!
    Adam Funk, Jul 30, 2006
    #9
  10. Adam Funk

    Adam Funk Guest

    On 2006-07-30, John W. Krahn <> wrote:

    >> The Perl program runs and prints the plot to the screen. I'm about to
    >> modify it to use backticks
    >> $gnuplot_output = `gnuplot $cmd_file`
    >> but I can't believe there isn't a better way than what I've done to
    >> send a list of commands to gnuplot and get the plot back.


    > You can probably do what you want with IPC::Open2 or Expect.
    >
    > perldoc IPC::Open2
    > perldoc Expect


    Those look quite useful, especially IPC::Open2 (which looks easier to
    use).

    I've got my program to work using Theo's suggestions, but I'll bear
    those in mind for the future.

    Thanks,
    Adam
    Adam Funk, Jul 31, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard
    Replies:
    7
    Views:
    449
  2. yeehaw

    ascii art for text

    yeehaw, Mar 1, 2007, in forum: HTML
    Replies:
    3
    Views:
    397
    yeehaw
    Mar 3, 2007
  3. Howie
    Replies:
    3
    Views:
    562
    Howie
    Dec 12, 2005
  4. Replies:
    1
    Views:
    245
  5. Emilio Mayorga
    Replies:
    6
    Views:
    314
    Martien Verbruggen
    Oct 8, 2003
Loading...

Share This Page