Pages

Monday, January 30, 2012

Real-Time Animation: bash & gnuplot

If you are using gnuplot to "plot" (I know, that is a surprise) data after the ill-computer calculations but still don't want to print a file and then: open gnuplot  or run a script (i'm getting bored even writing this) you can use bash to do it. Easy. Say your program prints $x$-$y$ points to the console each time you call it

~$ ./program < initial_data.dat
1 2 
3 4
7 5
8 6

then you make bash do this

#!/bin/bash
for((i=0;i<1;i++))
do 
echo "plot '-' u 1:2" 
echo "./program < initial_data.dat" | bash
echo "e"
done | gnuplot -persist
echo "Finished!"


Example. I don't really have a program to show you this and, honestly, I don't want to do it, so I will use bash directly to plot something with gnuplot. Let's try to create an animated gif on gnuplot for a sinus varying its wavelength.

#!/bin/bash
for((i=0;i<1;i++))
do 
echo "set term gif animate delay 40 enhanced;"
echo "set output \"sin_out.gif\";"

for ((k=0;k<11; k++)) do
echo "plot sin($k*x)" 
done

done | gnuplot -persist
echo "Finished!"

PD. The reason why I used a loop gnuplot need to be fed "|" with all the script at once. Byeee

No comments:

Post a Comment