代码(带 Beamer 覆盖)

代码(带 Beamer 覆盖)

我正在使用 beamer 制作演示文稿。假设我们有两个函数 $f$ 和 $g$ 需要绘制在一张图片中。我想制作以下效果。首先,显示函数 $f$,然后在 0.5 秒或 1 秒后,在同一张图片中显示函数 $g$,而函数 $f$ 现在消失,也就是说,只剩下函数 $g$。

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

假设函数$f$和$g$分别存储在文件function_f.txt和function_g.txt中。

有人知道怎么做吗?非常感谢。

答案1

如果你对“手动动画”幻灯片没意见,即通过点击鼠标/键盘/指针进行转换,那么你可以设置一个允许 Beamer 叠加规范的 TikZ 样式,如下这个答案。以下示例说明了如何叠加多个命令。请注意命令的\addplot键的使用。visible on=<num>\addplot

代码(带 Beamer 覆盖)

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

然而,如果你追求的是真正的动画,那么animate包裹是一个选项。以下示例显示了如何执行此操作。

代碼(附有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}

输出

在此处输入图片描述

相关内容