Tux

...making Linux just a little more fun!

2-cent Tip: Piping to GNU Plot from C

Amit Saha [amitsaha.in at gmail.com]


Sun, 4 Oct 2009 16:03:05 +0530

Hello TAG:

Can this be a possible 2-cent tip?

Couple of things first up:

* GNU plot supports piping, So, echo "plot sin(x)" | gnuplot will plot the sin(x) function.

* However, the plot disappears even before you could see it. For that echo "plot sin(x)" | gnuplot -persist , is useful. It persists the GNU plot main window

The usefulness of the second point is that, if you have a "pipe descriptor" describing a pipe to the open GNU plot instance , you can plot more plots on the first plot, without opening a new GNU plot instance. We shall be using this idea in our code.

#include <stdio.h>
#define GNUPLOT "gnuplot -persist"
 
int main(int argc, char **argv)
{
        FILE *gp;
        gp = popen(GNUPLOT,"w"); /* 'gp' is the pipe descriptor */
        if (gp==NULL)
           {
             printf("Error opening pipe to GNU plot. Check if you have it! \n");
             exit(0);
           }
 
        fprintf(gp, "set samples 2000\n");
        fprintf(gp, "plot abs(sin(x))\n");
        fprintf(gp, "rep abs(cos(x))\n");
        fclose(gp);
 
return 0;
}

The above code will produce a comparative plot of absolute value of sin(x) and cos(x) on the same plot. The popen function call is documented at http://www.opengroup.org/pubs/online/7908799/xsh/popen.html. This code/idea should work on GCC and Linux and any other language and OS that supports piping.

Utility: If you have a application which is continuously generating some data, which you will finally plot, then you can plot the data for every new set of data- that gives a nice visualization about how the data is changing with the iterations of your application. This is a perfect way to demonstrate convergence to the best solutions in Evolutionary Algorithms, such as Genetic Algorithms.

Best, Amit

-- 
Journal: http://amitksaha.wordpress.com,
µ-blog: http://twitter.com/amitsaha


Top    Back


Amit Saha [amitsaha.in at gmail.com]


Sun, 4 Oct 2009 16:04:49 +0530

On Sun, Oct 4, 2009 at 4:03 PM, Amit Saha <amitsaha.in@gmail.com> wrote:

> Hello TAG:
>
> Can this be a possible 2-cent tip?
>
>
> Couple of things first up:
>
> * GNU plot supports piping, So, echo "plot sin(x)" | gnuplot will
> plot the sin(x) function.
> * However, the plot disappears even before you could see it. For
> that echo "plot sin(x)" | gnuplot -persist , is useful. It persists
> the GNU plot main window
>
> The usefulness of the second point is that, if you have a "pipe
> descriptor" describing a pipe to the open GNU plot instance , you can
> plot more plots on the first plot, without opening a new GNU plot
> instance. We shall be using this idea in our code.
>
> <code>
>
> #include <stdio.h>
> #define GNUPLOT "gnuplot -persist"
>
> int main(int argc, char **argv)
> {
>        FILE *gp;
>        gp = popen(GNUPLOT,"w"); /* 'gp' is the pipe descriptor */
>        if (gp==NULL)
>           {
>             printf("Error opening pipe to GNU plot. Check if you have it! \n");
>             exit(0);
>           }
>
>        fprintf(gp, "set samples 2000\n");
>        fprintf(gp, "plot abs(sin(x))\n");
>        fprintf(gp, "rep abs(cos(x))\n");
>        fclose(gp);
>
> return 0;
> }
>
> </code>
>
> The above code will produce a comparative plot of absolute value of
> sin(x) and cos(x) on the same plot.  The popen function call is
> documented at http://www.opengroup.org/pubs/online/7908799/xsh/popen.html.
> This code/idea should work on GCC and Linux and any other language and
> OS that supports piping.
>
> Utility: If you have a application which is continuously generating
> some data, which you will finally plot, then you can plot the data for
> every new set of data- that gives a nice visualization about how the
> data is changing with the iterations of your application. This is a
> perfect way to demonstrate convergence to the best solutions in
> Evolutionary Algorithms, such as Genetic Algorithms.
>

Btw, its already up on my blog, if that matters.

-Amit

-- 
Journal: http://amitksaha.wordpress.com,
µ-blog: http://twitter.com/amitsaha


Top    Back


Thomas Adam [thomas.adam22 at gmail.com]


Sun, 4 Oct 2009 11:59:28 +0100

On Sun, Oct 04, 2009 at 04:03:05PM +0530, Amit Saha wrote:

> Utility: If you have a application which is continuously generating
> some data, which you will finally plot, then you can plot the data for
> every new set of data- that gives a nice visualization about how the
> data is changing with the iterations of your application. This is a
> perfect way to demonstrate convergence to the best solutions in
> Evolutionary Algorithms, such as Genetic Algorithms.

Or just use a named FIFO at the shell:

mkfifo /tmp/gnuplot
while :; do (gnuplot -persist) < /tmp/gnuplot; done

Then you need only do:

echo "plot sin(x)" > /tmp/gnuplot

... for as many times as you like. And as it's at the shell, you could even use a heredoc, etc.

-- Thomas Adam

-- 
"It was the cruelest game I've ever played and it's played inside my head."
-- "Hush The Warmth", Gorky's Zygotic Mynci.


Top    Back


Amit Saha [amitsaha.in at gmail.com]


Sun, 4 Oct 2009 17:56:24 +0530

On Sun, Oct 4, 2009 at 4:29 PM, Thomas Adam <thomas.adam22@gmail.com> wrote:

> On Sun, Oct 04, 2009 at 04:03:05PM +0530, Amit Saha wrote:
>> Utility: If you have a application which is continuously generating
>> some data, which you will finally plot, then you can plot the data for
>> every new set of data- that gives a nice visualization about how the
>> data is changing with the iterations of your application. This is a
>> perfect way to demonstrate convergence to the best solutions in
>> Evolutionary Algorithms, such as Genetic Algorithms.
>
> Or just use a named FIFO at the shell:
>
> ```
> mkfifo /tmp/gnuplot
> while :; do (gnuplot -persist) < /tmp/gnuplot; done
> '''
>
> Then you need only do:
>
> ```
> echo "plot sin(x)" > /tmp/gnuplot
> '''
>
> ... for as many times as you like. And as it's at the shell, you could even
> use a heredoc, etc.

Cool. Thanks for adding to it!

Best, Amit

-- 
Journal: http://amitksaha.wordpress.com,
µ-blog: http://twitter.com/amitsaha


Top    Back