pgfplots + animate:复杂的逐行动画

pgfplots + animate:复杂的逐行动画

我想要一个有 80 条对角线的矩形:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
%------------------------
%% Options
%------------------------
    width= 100mm,
    %height = 50mm,
    xmax=70,
    xmin=0,
    ymax=50,
    ymin=0,
    axis x line=bottom,
    axis y line=left,
    enlargelimits
]
%------------------------
%% Define Clipping
%------------------------
\pgfplotsextra{%
    \clip (axis cs:0,0) rectangle (axis cs:60,40);
    % http://www.latex-community.org/forum/viewtopic.php?f=45&t=22837&start=0
}   
%------------------------    
%% Loop
%------------------------
\foreach \i in {1,2,...,80} {
\addplot[
domain=0:60,
line width=1pt,
]
  {4/6*x+40-\i}; 
}
%------------------------
%% Draw retangle
%------------------------
\draw[red,line width=2pt] (axis cs:0,0) rectangle (axis cs:60,40);  
\end{axis}     
\end{tikzpicture}

\end{document}

在此处输入图片描述

现在我有一个数据文件,看起来像

4, % Line 1 is the top left corner
5,
50,
51,
52,
6,
7,
8,
9,
10,
22,
...

现在我希望这 80 行以该顺序逐行出现在动画中。我手动制作了一个动画 gif 来显示所需的输出:

在此处输入图片描述

但从这里开始我还没有正确的想法来继续下去。

杰克的解决方案

感谢大家的回答!我接受 Jake 的回答,因为它使用了一个对我来说很重要的外部文件。我的真实示例超过 1200 行。以下是解决方案(Jake 的答案缺少文件内容环境):

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{animate}

\usepackage{filecontents}

\begin{filecontents}{index.dat}
4,
5,
50,
51,
52,
6,
7,
8,
9,
10,
22,
\end{filecontents}

\begin{document}

\pgfplotstableread[col sep=comma]{index.dat}\indextable

\begin{animateinline}[controls]{2}
\multiframe{11}{imax=0+1}{
\begin{tikzpicture}
\begin{axis}[
%------------------------
%% Options
%------------------------
    width= 100mm,
    %height = 50mm,
    xmax=70,
    xmin=0,
    ymax=50,
    ymin=0,
    axis x line=bottom,
    axis y line=left,
    enlargelimits
]
%------------------------
%% Define Clipping
%------------------------

\pgfplotsextra{%
    \clip (axis cs:0,0) rectangle (axis cs:60,40);
    % http://www.latex-community.org/forum/viewtopic.php?f=45&t=22837&start=0
}   
%------------------------    
%% Loop
%------------------------

\foreach\i in {0,...,\imax}{
\pgfplotstablegetelem{\i}{[index]0}\of{\indextable}
\edef\currentindex{\pgfplotsretval}
\addplot[
domain=0:60,
line width=1pt,
]
  {4/6*x+40-\currentindex};
}

%------------------------
%% Draw retangle
%------------------------
\draw[red,line width=2pt] (axis cs:0,0) rectangle (axis cs:60,40);  
\end{axis}     
\end{tikzpicture}}
\end{animateinline}

\end{document}

结果如下:

在此处输入图片描述

答案1

您可以将包含有序值的数据文件读入 PGFPlots 表宏,然后在环境中循环遍历该数据文件animateinline

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{animate}

\usepackage{filecontents}


\begin{document}

\pgfplotstableread[col sep=comma]{index.dat}\indextable

\begin{animateinline}{2}
\multiframe{10}{imax=0+1}{
\begin{tikzpicture}
\begin{axis}[
%------------------------
%% Options
%------------------------
    width= 100mm,
    %height = 50mm,
    xmax=70,
    xmin=0,
    ymax=50,
    ymin=0,
    axis x line=bottom,
    axis y line=left,
    enlargelimits
]
%------------------------
%% Define Clipping
%------------------------
\pgfplotsextra{%
    \clip (axis cs:0,0) rectangle (axis cs:60,40);
    % http://www.latex-community.org/forum/viewtopic.php?f=45&t=22837&start=0
}   
%------------------------    
%% Loop
%------------------------

\foreach\i in {0,...,\imax}{
\pgfplotstablegetelem{\i}{[index]0}\of{\indextable}
\edef\currentindex{\pgfplotsretval}
\addplot[
domain=0:60,
line width=1pt,
]
  {4/6*x+40-\currentindex};
}




%------------------------
%% Draw retangle
%------------------------
\draw[red,line width=2pt] (axis cs:0,0) rectangle (axis cs:60,40);  
\end{axis}     
\end{tikzpicture}}
\end{animateinline}

\end{document}

答案2

这适用于 beamer。循环在外面axis

\documentclass{beamer}

\usepackage{tikz}
\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}
\begin{tikzpicture}
\foreach \i [count=\j] in  {4,5,50,51,52,6,7} {
\begin{axis}[
%------------------------
%% Options
%------------------------
    width= 100mm,
    %height = 50mm,
    xmax=70,
    xmin=0,
    ymax=50,
    ymin=0,
    axis x line=bottom,
    axis y line=left,
    enlargelimits
]
%------------------------
%% Define Clipping
%------------------------
\pgfplotsextra{%
    \clip (axis cs:0,0) rectangle (axis cs:60,40);
    % http://www.latex-community.org/forum/viewtopic.php?f=45&t=22837&start=0
}   
%------------------------
%% Draw retangle
%------------------------
\draw[red,line width=2pt] (axis cs:0,0) rectangle (axis cs:60,40);  

\addplot[
domain=0:60,
line width=1pt, visible on=<\j->] 
  {4/6*x+40-\i}; 
\end{axis}}
\end{tikzpicture}
\end{frame}
\end{document}

稍后我会制作一个 gif 并添加更多信息。

在此处输入图片描述 在此处输入图片描述

答案3

这是为了更灵活而改变顺序列表的尝试。App是一个接受两个参数的命令,参数 #1= 帧数和 #2= 线条绘制顺序。请注意,列表\multiframe{#1}中的 #1 应该是最后一个App

App命令本身是一个绘制非对角线的 whiledo 循环。

请剪切并粘贴来显示动画的发生。

在此处输入图片描述

在此处输入图片描述

代码

\documentclass[border=10pt]{standalone}
\usepackage{animate}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}


\newcounter{n}
\setcounter{n}{01}
\newcommand{\App}[2]{
\whiledo{\value{n}=#1 \and \icount=#1}{
 \stepcounter{n}            % #1= # of frame
     \foreach \i in {#2} {  % #2=list
\addplot[
domain=0:60,
line width=1pt,
]
  {4/6*x+40-\i}; 
}}}


\begin{document}
%\begin{center}
\begin{animateinline}[loop,poster =first, controls]{1}
\multiframe{10}{icount=0+1}
{
\begin{tikzpicture}
\begin{axis}[
%------------------------
%% Options
%------------------------
    width= 100mm,
    %height = 50mm,
    xmax=70,
    xmin=0,
    ymax=50,
    ymin=0,
    axis x line=bottom,
    axis y line=left,
    enlargelimits
]
%------------------------
%% Define Clipping
%------------------------
\pgfplotsextra{%
    \clip (axis cs:0,0) rectangle (axis cs:60,40);
    % http://www.latex-community.org/forum/viewtopic.php?f=45&t=22837&start=0
}   
%------------------------    
%% Loop
%------------------------
\App{1}{4}
\App{2}{4,5}
\App{3}{4,5,50}
\App{4}{4,5,50,51}
\App{5}{4,5,50,51,52}
\App{6}{4,5,50,51,52,6}
\App{7}{4,5,50,51,52,6,7,8}
\App{8}{4,5,50,51,52,6,7,8,9}
\App{9}{4,5,50,51,52,6,7,8,9,10}
\App{10}{4,5,50,51,52,6,7,8,9,10,22}
%------------------------
%% Draw retangle
%------------------------
\draw[red,line width=2pt] (axis cs:0,0) rectangle (axis cs:60,40);  
\end{axis}     
\end{tikzpicture}
}
\end{animateinline}
%\end{center}
\end{document}

相关内容