## Posts Tagged ‘**gnuplot**’

## pdf to gif conversion and optimization

There have been several occasions that I need to generate some gif animations from a bunch of pdf plots (generated by gnuplot’s tikz terminal), so the following procedure merits documenting. Note that `convert` is part of ImageMagick, and `gifsicle` is a gif optimization tool.

- Converting pdfs to png:

`for f in *pdf; do convert -verbose -density 150x150 $f `basename $f .pdf`.png; done` - combining png to gif (and optimize):

`convert -verbose +dither -layers Optimize -colors 32 -delay 5 -loop 0 -dispose previous *png output.gif` - if there are too many *png files, it’s probably better to divide and conquerer: assuming png files are frame_000 upto frame_139, then first:

`for i in `seq -f "%02g" 0 13`; do convert -verbose +dither -layer Optimize -delay 5 -loop 0 -dispose previous frame_${f}?.png frame_${f}.gif; done`

then

`convert -verbose frame_??.gif all.gif` - notice that I’ve dropped the
`-color 32`, b/c in my case it actually increases the resulting file size. - further optimization (YMMV) using gifsicle:

gifsicle -O3 output.gif -o output_optimized.gif

cf:

p.s., a related operation is to combine say every 10th of the pdfs for closeup inspection:

`pdfjam frame_??0.pdf --papersize '{12in,9in}' --outfile frames.pdf`

`pdfjam is a frontend to the latex package "pdfpages". The --papersize I used is the same as the one I had used in gnuplot when generating these plots (set term tikz size 12in,9in ...).`

## quick notes on using gnuplot’s lua terminal

In gnuplot,

set term tikz fulldoc color solid createstyle

set output 'tmp.tex'

p x**2 tit '$x^2$'

then in shell,

pdflatex tmp.tex

NB:

(1) ‘createstyle’ is crucial, otherwise tex will complain about missing ‘gnuplot-lua-tikz.sty’. This doesn’t seem to be mentioned in the first several google results

(2) I have to comment out the ‘utf8x’ package in the resulting tmp.tex. That may just be a problem with my tex environment

## gnuplot: different color on different segments of a curve

I’m not sure what’s the proper description, but here’s a task at hand and how it is done in gnuplot:

suppose I have a datafile with 3 columns. I can plot columns 1 and 2 as a curve:

gnuplot> p 'datfile' u 1:2

In my problem, the 3rd column indicates whether the entry is “localized on the left side”, which will have `abs($3)>1`, or the “right side”, having `abs($3)<1`. I want the curve to be plotted in one color (say red) for the “left side” segment(s), and another color (say green) for the “right side” segment(s)

somewhere in gnuplot’s manual, it says it will silently drop data if they are unplottable, and gives an example that `1/0` is unplottable (division by zero). so here’s the idea:

define the “above” and “below” function:

gnuplot> above(x,a) = x > a ? 1 : 1/0

gnuplot> below(x,a) = x < a ? 1 : 1/0

then I can use them to pick out the left/right segments:

gnuplot> p '' u 1:($2*above(abs($3),1)) w l lt rgb 'red' lw 2 tit "localized to the right"

gnuplot> rep u 1:($2*below(abs($3),1)) w l lt rgb 'green' lw 2 tit "localized to the left"

there is a minor problem: for data files, the segments with different colors are always separated by a small gap b/c neither `above` nor `below` would cover that gap. Haven’t thought about how to solve this yet, probably a clever redef of `above` and `below` could do the job.