# Animate PGF Plot from discrete data

I have a tikz picture that contains data points (only a part shown)

\documentclass{article}
\usepackage{pgfplots}
\newlength\figureheight
\newlength\figurewidth
\setlength{\figurewidth}{8cm}
\setlength{\figureheight}{4.5cm}
\begin{document}
\begin{tikzpicture}

\begin{axis}[%
/pgf/number format/.cd,
use comma,
1000 sep={},
width=0.951\figurewidth,
height=\figureheight,
at={(0\figurewidth,0\figureheight)},
scale only axis,
xmin=-1,
xmax=1,
xlabel style={font=\color{white!15!black}},
xlabel={xlabel},
ymin=0,
ymax=1,
ylabel style={font=\color{white!15!black}},
ylabel={ylabel},
axis background/.style={fill=white}
]
table[row sep=crcr]{%
-1  0\\
-0.987  0.001\\
-0.823  0.15\\
-0.713  0.4\\
-0.684  0.5\\
-0.542  0.6\\
-0.478  0.65\\
-0.3544 0.7\\
-0.254  0.8\\
-0.14   0.9\\
0   1\\
0.057774545 0.9\\
0.15794 0.8\\
0.258105455 0.7\\
0.358270909 0.65\\
0.458436364 0.6\\
0.558601818 0.5\\
0.658767273 0.4\\
0.758932727 0.15\\
0.859098182 0.05\\
0.959263636 0.001\\
1   0\\
};
\end{axis}
\end{tikzpicture}
\end{document}


The data is generated from measurements and can't be fitted (easily) with a single function or at least I'd like to avoid that.

Is there a nice way to animate (line plot) the data stepwise or in chunks of n points?

I would like to avoid manually generating

Image-01

    \addplot [color=black, forget plot]
table[row sep=crcr]{%
-1  0\\
%-0.987  0.001\\
%-0.823  0.15\\
%-0.713  0.4\\
%-0.684  0.5\\
%-0.542  0.6\\
...
};


Image-02

    \addplot [color=black, forget plot]
table[row sep=crcr]{%
-1  0\\
-0.987  0.001\\
%-0.823  0.15\\
%-0.713  0.4\\
%-0.684  0.5\\
%-0.542  0.6\\
...
};


etc. in order to loop those images. I would prefer an automated solution.

I would be using the beamer class and compile with XeLaTeX if this information makes a difference.

• Please make a minimal working example (MWE) Jan 31 '17 at 22:54
• I don't have a MWE. I elaborated on the method I would like to avoid with more detailed code if that helps. Jan 31 '17 at 23:07
• It would be useful if you could add a compilable example that will produce the complete plot, including all the relevant packages. Not everyone who knows how to animate things in beamer also speaks pgfplots or wants to learn it just to find out what packages are needed. Jan 31 '17 at 23:16
• That makes sense! I didn't think of that and thought basically any plot will do for me as an example as long as it containts negative, not evenly spaced x values. Feb 1 '17 at 0:21

A animation with beamer on-board tools:

The select macro was borrowed from https://tex.stackexchange.com/a/199396/36296

\documentclass{beamer}
\usepackage{pgfplots}
\newlength\figureheight
\newlength\figurewidth
\setlength{\figurewidth}{8cm}
\setlength{\figureheight}{4.5cm}

% Style to select only points from #1 to #2 (inclusive)
\pgfplotsset{select coords between index/.style 2 args={
x filter/.code={
\ifnum\coordindex<#1\def\pgfmathresult{}\fi
\ifnum\coordindex>#2\def\pgfmathresult{}\fi
}
}}

\makeatletter
\newcommand{\slide}{\the\beamer@slideinframe}
\makeatother

\begin{document}
\begin{frame}
\animate<1-22>
\transduration<1-22>{0.5}
\begin{tikzpicture}
\begin{axis}[%
/pgf/number format/.cd,
use comma,
1000 sep={},
width=0.951\figurewidth,
height=\figureheight,
at={(0\figurewidth,0\figureheight)},
scale only axis,
xmin=-1,
xmax=1,
xlabel style={font=\color{white!15!black}},
xlabel={xlabel},
ymin=0,
ymax=1,
ylabel style={font=\color{white!15!black}},
ylabel={ylabel},
axis background/.style={fill=white}
]
\addplot [color=black, forget plot,select coords between index={0}{\slide}]
table[row sep=crcr]{%
-1  0\\
-0.987  0.001\\
-0.823  0.15\\
-0.713  0.4\\
-0.684  0.5\\
-0.542  0.6\\
-0.478  0.65\\
-0.3544 0.7\\
-0.254  0.8\\
-0.14   0.9\\
0   1\\
0.057774545 0.9\\
0.15794 0.8\\
0.258105455 0.7\\
0.358270909 0.65\\
0.458436364 0.6\\
0.558601818 0.5\\
0.658767273 0.4\\
0.758932727 0.15\\
0.859098182 0.05\\
0.959263636 0.001\\
1   0\\
};
\end{axis}
\end{tikzpicture}
\pause[20]
\end{frame}
\end{document}


• Great work! Almost exactly what I am looking for. Is there a way the animation can circle through all frames by itself, i.e. after opening the slide or clicking on it? For my presentation I have a lot more data points and I wouldn't like to click manually through all of them. Feb 1 '17 at 8:39
• @idkfa The code above already should do that. With adobe reader in presentation mode it certainly works. At the moment, it's rather slow, but you can adjust the speed with \transduration<1-22>{0.5} Feb 1 '17 at 9:47

Here is an animation of some datapoints from a table. For the animation I use the animate-package.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{animate}
\usepackage{tikz}

\begin{filecontents}{mydata.data}
X Y
0 2
1 4
2 2
3 5
4 4
5 1
\end{filecontents}

\begin{document}
\begin{animateinline}{2}% frames per second
\multiframe{5}{ix=1+1}{% the +1 is the step size
\begin{tikzpicture}
\begin{axis}[
xlabel={X-Name},
ylabel={Y-Name},
xmin=0, xmax=5,
ymin=1, ymax=5,
]
\addplot[restrict x to domain=0:\ix,red] table [x index=0,y index=1]{mydata.data};
\end{axis}
\end{tikzpicture}
}
\end{animateinline}
\end{document}


The result is (start and end view of the animation):

Requirements for animate:

• pdfTEX, version >= 1.20 or LuaTEX for direct PDF output
• Ghostscript, version >= 9.15 or Adobe Distiller for PS to PDF conversion
• dvipdfmx, version >= 20080607 for DVI to PDF conversion

Edit: For looping the images afterwards you can set the option loop for the animate-package: \usepackage[loop]{animate}.

• Thanks! XeLaTeX is able to compile your example, however if I change ´ix=-1+0.01´ I get package calc error .' invalid at this point. My data contains negative values and the x coordinates are not evenly spaced. I can't compile with LuaLaTex (max-print-line 120 unknown -> lualatex.fmt missing -> pgfsys-luatex.def not found -> ...) Seems it does not work out of the box with Texniccenter and Miktex Feb 1 '17 at 0:00
• The prefix i of the loop variable ix declares it an integer variable. For non-integers, replace ix with rx. Feb 1 '17 at 9:57

Since the OP uses the standard article class which does not provide the stepping facilities of beamer, this example makes use of the animate package to create the animated graph on a single document page (using samcarter's code with slight modifications):

\documentclass{article}

\usepackage{pgfplots}
\usepackage{animate}

\newlength\figureheight
\newlength\figurewidth
\setlength{\figurewidth}{8cm}
\setlength{\figureheight}{4.5cm}

% Style to select only points from #1 to #2 (inclusive)
\pgfplotsset{select coords between index/.style 2 args={
x filter/.code={
\ifnum\coordindex<#1\def\pgfmathresult{}\fi
\ifnum\coordindex>#2\def\pgfmathresult{}\fi
}
}}

\begin{document}
\begin{animateinline}[controls]{2} % 2 frames per sec
\multiframe{22}{idx=1+1}{  % 22 frames for 22 line segments
\begin{tikzpicture}      % idx=1, 2, ..., 22
\begin{axis}[%
/pgf/number format/.cd,
use comma,
1000 sep={},
width=0.951\figurewidth,
height=\figureheight,
at={(0\figurewidth,0\figureheight)},
scale only axis,
xmin=-1,
xmax=1,
xlabel style={font=\color{white!15!black}},
xlabel={xlabel},
ymin=-0.1,
ymax=1,
ylabel style={font=\color{white!15!black}},
ylabel={ylabel},
axis background/.style={fill=white}
]
color=black, forget plot,select coords between index={0}{\idx},
unbounded coords=jump
] table[row sep=crcr]{%
-1  0\\
-0.987  0.001\\
-0.823  0.15\\
-0.713  0.4\\
-0.684  0.5\\
-0.542  0.6\\
-0.478  0.65\\
-0.3544 0.7\\
-0.254  0.8\\
-0.14   0.9\\
0   1\\
0.057774545 0.9\\
0.15794 0.8\\
0.258105455 0.7\\
0.358270909 0.65\\
0.458436364 0.6\\
0.558601818 0.5\\
0.658767273 0.4\\
0.758932727 0.15\\
0.859098182 0.05\\
0.959263636 0.001\\
1   0\\
};
\end{axis}
\end{tikzpicture}
}
\end{animateinline}
\end{document}

• I apologize, I copied the MWE from my document to show the plot as samcarter requested ("add a compilable example that will produce the complete plot, including all the relevant packages") In the last paragraph of my question I stated that I'd use beamer + xelatex. Thank you nontheless! I'll try and recreate your solution as well. Feb 1 '17 at 10:33
• No need to apologize. This works with any document class and with xelatex` too. Feb 1 '17 at 11:06