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 來顯示所需的輸出:

在此輸入影像描述

但從這裡開始我沒有正確的想法繼續下去。

傑克的解決方案

感謝您的回答!我接受傑克的回答,因為它使用對我來說很重要的外部文件。我的真實範例超過 1200 行。這是解決方案(傑克的答案缺少文件內容環境):

\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

這適用於投影機。循環在外面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= 線條繪製的順序。請注意,#1\multiframe{#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}

相關內容