# Animate pgfplot graph using animate package

After googling how to animate a plot I found different ways that I didn't get, I've done some animations before using this method. My question is it possible to use the same method to animate the following plot ?

( I need this animation in few days so I'm not able to learn other methods )

\documentclass[tikz]{standalone}
\usetikzlibrary{animations}
\usepackage{animate}
\usepackage{ifthen}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{document}

\foreach \x in {0,1,...,20} {
\begin{tikzpicture}
\useasboundingbox (-3,-5) rectangle (9,10);
\begin{axis}[%title={test},
legend style={font=\fontsize{3}{3}\selectfont},
legend pos=south east,
axis lines=center,
domain=0.2:8,
xtick={0,1,...,7},
ytick={0,1,...,4},
samples=501,
xlabel={}]

\end{axis}
\end{tikzpicture}
}
\end{document}

• Exactly what do you mean for "animate a plot"? – vi pa Sep 19 '20 at 10:52
• Like this tex.stackexchange.com/questions/56461/… @vipa – Hibou Sep 19 '20 at 10:53
• Do you want a GIF in which the plot is draw point by point? Or an animation of the plot on a PDF file? – vi pa Sep 19 '20 at 10:58
• I want a GIF in which the plot is drawn point by point @vipa – Hibou Sep 19 '20 at 11:00
• For usage in a WEB page, SVG would also be an option. – AlexG Sep 19 '20 at 11:33

For the problem at hand, the upper bound of the plotting domain needs to be animated.

\documentclass{standalone}
%\documentclass[dvisvgm]{standalone} % latex <file>.tex ; dvisvgm --font-format=woff2 --zoom=-1  --exact <file>.dvi

\usepackage[T1]{fontenc}
\usepackage{animate}
\usepackage{pgfplots}

\begin{document}

\begin{animateinline}[controls]{20}
\multiframe{81}{rXmax=0+0.1}{
\begin{tikzpicture}
\begin{axis}[
axis lines=center,
domain=0.001:\rXmax,
xtick={0,1,...,8},
xmax=8.4,
ymax=1.6,
samples=501
]

\end{axis}
\end{tikzpicture}
}
\end{animateinline}

\end{document}

• I have found that the conversion to GIF is much better if you add a background to the tikzpicture in the standalone class. – vi pa Sep 20 '20 at 9:09
• @vipa You can do that with convert. Add option -alpha remove. – AlexG Sep 20 '20 at 9:34

My solution use gnuplot and must be compiled (only the first time for get the table of the points of the curve) using pdflatex.exe -shell-escape %.tex.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{math,backgrounds}
\begin{document}

\foreach \xDom in {0.2,0.3,...,7.2} {
\begin{tikzpicture}[background rectangle/.style={fill=white},show background rectangle]
\tikzmath{
\samplePoints= int(\xDom*100);
\idFile= int(\xDom*10);
}
\begin{axis}[%title={test},
legend style={font=\fontsize{3}{3}\selectfont},
legend pos=south east,
axis lines=center,
domain=0.2:\xDom,
xmin=0.2,xmax=7.5,
ymin={1-exp(-0.2)*cos(3*deg(0.2))},
ymax=1.5,
xtick={0,1,...,7},
ytick={0,1,...,4},
samples=\samplePoints,
xlabel={}]
\draw[gray, dashed] (axis cs:0,1) -- (axis cs:7.2,1);

\end{axis}
\end{tikzpicture}
}
\end{document}


As can be see I have add a background to the tikzpicture, because I have found that conversion to GIF is much better in this way (less aliased). After the compilation of the LaTeX code you must convert the multi page PDF to GIF using ImageMagick. I use the following istruction in the command line:

magick convert -verbose -density 300 -strip -resize 50% -layers OptimizePlus -delay 30 *.pdf myGIF.gif


If you don't use Windows omit the initial magick

The result is:

Edit: As AlexG say it's not neccessary the background, you can use the flag -alpha remove in convert. Therefore an improved code (with some other changes) is:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{math}
\begin{document}

\foreach \xDom [count=\idFile] in {0.2,0.21,...,7.2} {
\begin{tikzpicture}
\tikzmath{
\samplePoints= ifthenelse(\xDom==0.2, 1, int(\xDom*100));
}
\begin{axis}[%title={test},
legend style={font=\fontsize{3}{3}\selectfont},
legend pos=south east,
axis lines=center,
domain=0.2:\xDom,
xmin=0.2,xmax=7.5,
ymin={1-exp(-0.2)*cos(3*deg(0.2))},
ymax=1.5,
xtick={0,1,...,7},
ytick={0,1,...,4},
samples=\samplePoints,
xlabel={}]
\draw[gray, dashed] (axis cs:0,1) -- (axis cs:7.2,1);

\end{axis}
\end{tikzpicture}
}
\end{document}


As before this must be compiled with pdflatex.exe -shell-escape %.tex for get the multipage PDF and then with:

magick convert -verbose -density 300 -strip -resize 40% -alpha remove -layers OptimizePlus -delay 1 *.pdf myGif.gif


And the result is:

• Thank you ! Just a question, is it possible to have a smoother animation ? @vipa – Hibou Sep 20 '20 at 13:22
• @Hibou See my edit answer. – vi pa Sep 21 '20 at 18:14