The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

carminat (49) [Avatar] Offline
#1
I am trying to produce an animated gif: I can do it automatically, but I would like to obtain the same result without writing the .png files on disk (i.e. using pipes as in sect. 4.5.4).

Unfortunately, I do not understand the syntax I have to use.

I include my current script to explain what I am trying to get.


################################### myscript
set terminal pngcairo size 800,500 enhanced

### settings
#
set key top center
set xrange [0:1]
set yrange [0:1]
set size ratio -1
set xzeroaxis
set yzeroaxis
set sample 1000
set style fill transparent solid .4

## creation of .png files
#
do for [n=1:40]{
## animo is the directory where png files are stored
outfile = sprintf('animo/frame%03.0f.png',n)
set output outfile
plot [-.1:2][-.1:1.1] "+"u 1smilie0.0001)smilie0.1) w filledcurves lt 5 t "", for [jj=1:n] 2*jj*x/(1+jj**2*x**2) lt 0 t "", 2*n*x/(1+n**2*x**2) lt 7 title sprintf("n=%i", n)
}

## packing files in a gif
#
system('convert -delay 20 -loop 0 ./animo/*.png newimage.gif')
system('firefox newimage.gif')
philipp.janert (286) [Avatar] Offline
#2
You can use the "animate" option with the gif terminal. It's
important to send all plots in the sequence to the SAME file,
like so:

set output "out.gif"
set terminal animated
plot sin(x)
plot sin(2*x)
plot sin(3*x)
set output

The delay and number of iterations can be changed in the
set terminal command - check "help gif".
carminat (49) [Avatar] Offline
#3
I guess that the command to invoke animated gif should be
set terminal gif animate


In fact this script works for me:

set output "out.gif"
set terminal gif animate

plot sin(x)
plot sin(2*x)
plot sin(3*x)
set output


However:
1) the graphic quality looks quite poor (it's much better to pass through
the .png files)
2) it seems gnuplot is not able to do the same task with nested loops (which is quite puzzling), for instance the following script does not give an animation:

set output "all.gif"
set terminal gif animated

### settings
#
set key top center
set xrange [0:1]
set yrange [0:1]
set size ratio -1
set xzeroaxis
set yzeroaxis
set sample 1000
set style fill transparent solid .4

do for [n=1:40]{
    plot [-.1:2][-.1:1.1] "+"u 1:(0.0001):(0.1) w filledcurves lt 5 t "", for [jj=1:n] 2*jj*x/(1+jj**2*x**2) lt 0 t "", 2*n*x/(1+n**2*x**2) lt 7 title sprintf("n=%i", n)
}
set term
philipp.janert (286) [Avatar] Offline
#4
1) The "gif" terminal is based on the GD library, which is by now rather dated.
For instance, it does not use anti-aliasing, which results in the perceived poor quality.

2) You can't use an inline loop; instead, you have to use an explicit loop:

do for[j=1:10] { plot sin(j*x) }

will work as expected.

Inline loops are really shorthands for a SINGLE plot command, with all the individual
curves to plot in a single line - essentially, a short-hand for
plot sin(x), sin(2*x), sin(3*x), ...

In contrast, the explicit loop makes a separate call to plot every time through the loop,
which is what you want in this case. (The same issue arises with mulitplot, by the way.)