5

I am using beamer to create a presentation. Suppose we have two functions $f$ and $g$ to be plotted in one picture. I want to make the following effect. First, the function $f$ is displayed, and after, let's say, 0.5 or 1 second, the function $g$ is displayed in the same picture, and the function $f$ now disappeared, that is, only the function $g$ is remained.

\begin{figure}[!htb]
\centering
\begin{tikzpicture}
\begin{axis}[grid=both,xmin=0,xmax=1,
xtick={0,1},
ymin=0,ymax=1,
ytick={0,1},
width=7cm]
\addplot[color=black,mark=none] table {function_f.txt};
\addplot[color=black,mark=none] table {function_g.txt};
\end{axis} 
\end{tikzpicture}
\end{figure}

Suppose that the functions $f$ and $g$ are stored in the files function_f.txt and function_g.txt, respectively.

Is there anyone know how to do this? Thanks a lot.

Richie
  • 487

1 Answers1

4

If you're okay with "manually animating" the slides, i.e. making transitions by clicking on the mouse/keyboard/pointer, then you can set a TikZ style that allows Beamer overlay specifications as in this answer. The following example illustrates how you can overlay several \addplot commands. Notice the use of key visible on=<num> for the \addplot command.

Code (with Beamer overlay)

\documentclass{beamer}
\usepackage{pgfplots}
\tikzset{
  invisible/.style={opacity=0},
  visible on/.style={alt={#1{}{invisible}}},
  alt/.code args={<#1>#2#3}{%
    \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
  },
}

\begin{document}
\begin{frame}
\centering
\begin{tikzpicture}
  \begin{axis}[ymin=0,ymax=1,enlargelimits=false]
    \addplot
    [blue!80!black,fill=blue,fill opacity=0.5,visible on=<2>]
    coordinates
    {(0,0.1) (0.1,0.15) (0.2,0.5) (0.3,0.62)
      (0.4,0.56) (0.5,0.58) (0.6,0.65) (0.7,0.6)
    (0.8,0.58) (0.9,0.55) (1,0.52)}
    |- (axis cs:0,0) -- cycle;
    \addplot
    [red,fill=red!90!black,opacity=0.5,visible on=<3>]
    coordinates
    {(0,0.25) (0.1,0.27) (0.2,0.24) (0.3,0.24)
      (0.4,0.26) (0.5,0.3) (0.6,0.23) (0.7,0.2)
    (0.8,0.15) (0.9,0.1) (1,0.1)}
    |- (axis cs:0,0) -- cycle;
    \addplot[green!20!black,visible on=<4>] coordinates
    {(0,0.4) (0.2,0.75) (1,0.75)};
  \end{axis}
\end{tikzpicture}
\end{frame}
\end{document}

However, if it is real animation you're after, then the animate package is an option. The following example shows how this can be done.

Code (with animate)

\documentclass{beamer}
\usepackage{pgfplots}
\usepackage{animate}
\newcommand\myplot[1]{
  \ifnum#1=1 
  \else\ifnum#1=2
    \addplot
    [blue!80!black,fill=blue,fill opacity=0.5]
    coordinates
    {(0,0.1) (0.1,0.15) (0.2,0.5) (0.3,0.62)
      (0.4,0.56) (0.5,0.58) (0.6,0.65) (0.7,0.6)
    (0.8,0.58) (0.9,0.55) (1,0.52)}
    |- (axis cs:0,0) -- cycle;
  \else\ifnum#1=3
    \addplot
    [red,fill=red!90!black,opacity=0.5]
    coordinates
    {(0,0.25) (0.1,0.27) (0.2,0.24) (0.3,0.24)
      (0.4,0.26) (0.5,0.3) (0.6,0.23) (0.7,0.2)
    (0.8,0.15) (0.9,0.1) (1,0.1)}
    |- (axis cs:0,0) -- cycle;
  \else\ifnum#1=4
    \addplot[green!20!black] coordinates
    {(0,0.4) (0.2,0.75) (1,0.75)};
  \else error
  \fi\fi\fi\fi
}
\begin{document}
\begin{frame}
\centering
\begin{animateinline}[autoplay,loop]{1}
  \multiframe{4}{i=1+1}{
    \begin{tikzpicture}
      \begin{axis}[ymin=0,ymax=1,enlargelimits=false]
        \myplot{\i}
      \end{axis}
    \end{tikzpicture}  
  }
\end{animateinline}
\end{frame}
\end{document}

Output

enter image description here

Herr K.
  • 17,946
  • 4
  • 61
  • 118
  • Hi Kevin, thanks a lot for your help. Can I ask one more question? Suppose that I got from MATLAB a file data_function.txt, there are 51 columns, and the structure is the following. The first column is always the for x-axis, the 2 to 51 columns are each for y-axis. So the first plot is (column 1, column 2), second (column 1, column 3), until (column 1, column 51). So we have 50 figures in total such that they appear one after another. Do you know how to modify your definition of \myplot, to make this possible? Thanks really much. – Richie Jun 13 '14 at 06:34
  • @Richie: Sorry for the late reply. In that case, it's even easier. Define \myplot as follows: \newcommand\myplot[1]{\addplot[color=black,mark=none] table[x index=0, y index=#1]{data_function.txt};. Also, don't forget to change the first argument of \multiframe from {4} to {50}. – Herr K. Jun 14 '14 at 00:32