2

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 ?

enter image description here

( 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={}]
\addplot [gray, dashed] {1};

\addplot [color=red] {1-exp(-\x)*cos(3*deg(\x))};

\end{axis}
\end{tikzpicture}
}
\end{document}
5
  • Exactly what do you mean for "animate a plot"?
    – vi pa
    Commented Sep 19, 2020 at 10:52
  • Like this tex.stackexchange.com/questions/56461/… @vipa
    – Hibou
    Commented Sep 19, 2020 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
    Commented Sep 19, 2020 at 10:58
  • I want a GIF in which the plot is drawn point by point @vipa
    – Hibou
    Commented Sep 19, 2020 at 11:00
  • For usage in a WEB page, SVG would also be an option.
    – AlexG
    Commented Sep 19, 2020 at 11:33

2 Answers 2

3

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
  ]
  \addplot [gray, dashed] {1};
  
  \addplot [color=red] {1-exp(-x)*cos(3*deg(x))};
  
  \end{axis}
  \end{tikzpicture}
}
\end{animateinline}

\end{document}
2
  • 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
    Commented Sep 20, 2020 at 9:09
  • @vipa You can do that with convert. Add option -alpha remove.
    – AlexG
    Commented Sep 20, 2020 at 9:34
1

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);

\addplot [color=red] gnuplot[id=gr\idFile] {1-exp(-x)*cos(3*x)};

\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:

enter image description here

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);

\addplot [color=red] gnuplot[id=gr\idFile] {1-exp(-x)*cos(3*x)};

\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:

enter image description here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .