# Animate a pgfplots 3d-plot

This question is related to This. but not the same I guess as here I want to animate the view.

This issue came up in my previous question here, I want to show the maximum and minimum of the function $\sin(x)\sin(y)$ to my students. I have plotted the surface in the beamer using pgfplots. Now I am wondering if there is a way to make a small animation of the viewing angle to show the peaks and drops on the surface?

Yes, here's one option using a \foreach loop and \only; the angle can be changed using the view option (or one of the similar options) for the axis environment:

\documentclass{beamer}
\usepackage{pgfplots}

\begin{document}

\begin{frame}
\centering
\begin{tikzpicture}
\foreach \Angle [count=\ti] in {30,60,120,150,210,240,300,330}
{
\only<\ti>{\begin{axis}[
view={\Angle}{30}]
{sin(x)*sin(y)};
\end{axis}}
}
\end{tikzpicture}
\end{frame}

\end{document}


Using ImageMagick and something like

convert -verbose -delay 120 -loop 0 -density 300 a.pdf a.gif


you can get an animation:

You can also use the animate package to produce directly the animation, but this only works with Adobe Reader, if I recall correctly.

• Sorry, is there supposed to be an example below the final line? If so, it’s missing. Sep 6, 2015 at 17:58
• @Lorehead No. That final line was a leftover that I've now removed. Sep 6, 2015 at 18:35

Here is the version using the animate package:

\documentclass{beamer}
\usepackage{pgfplots}
\usepackage{animate}
\begin{document}

\begin{frame}

\begin{animateinline}[poster=last, controls, palindrome]{12}%
\multiframe{29}{iAngle=80+10}{%
%iAngle = 80, 90, ..., 360 degrees
\begin{tikzpicture}
\begin{axis}[view={\iAngle}{30}]