Animar gráfico PGF a partir de dados discretos

Animar gráfico PGF a partir de dados discretos

Eu tenho uma imagem tikz que contém pontos de dados (apenas uma parte mostrada)

\documentclass{article}
\usepackage{pgfplots} 
\newlength\figureheight 
\newlength\figurewidth 
\setlength{\figurewidth}{8cm}
\setlength{\figureheight}{4.5cm}
\begin{document}
\begin{tikzpicture}

    \begin{axis}[%
    /pgf/number format/.cd,
            use comma,
            1000 sep={},
    width=0.951\figurewidth,
    height=\figureheight,
    at={(0\figurewidth,0\figureheight)},
    scale only axis,
    xmin=-1,
    xmax=1,
    xlabel style={font=\color{white!15!black}},
    xlabel={xlabel},
    ymin=0,
    ymax=1,
    ylabel style={font=\color{white!15!black}},
    ylabel={ylabel},
    axis background/.style={fill=white}
    ]
    \addplot [color=black, forget plot]
      table[row sep=crcr]{%
-1  0\\
-0.987  0.001\\
-0.823  0.15\\
-0.713  0.4\\
-0.684  0.5\\
-0.542  0.6\\
-0.478  0.65\\
-0.3544 0.7\\
-0.254  0.8\\
-0.14   0.9\\
0   1\\
0.057774545 0.9\\
0.15794 0.8\\
0.258105455 0.7\\
0.358270909 0.65\\
0.458436364 0.6\\
0.558601818 0.5\\
0.658767273 0.4\\
0.758932727 0.15\\
0.859098182 0.05\\
0.959263636 0.001\\
1   0\\
    };
    \end{axis}
    \end{tikzpicture}
\end{document} 

insira a descrição da imagem aqui

Os dados são gerados a partir de medições e não podem ser ajustados (facilmente) a uma única função ou pelo menos gostaria de evitar isso.

Existe uma boa maneira de animar (gráfico de linha) os dados passo a passo ou em pedaços de n pontos?

Eu gostaria de evitar gerar manualmente

Imagem-01

    \addplot [color=black, forget plot]
      table[row sep=crcr]{%
-1  0\\
%-0.987  0.001\\
%-0.823  0.15\\
%-0.713  0.4\\
%-0.684  0.5\\
%-0.542  0.6\\
...
    };

Imagem-02

    \addplot [color=black, forget plot]
      table[row sep=crcr]{%
-1  0\\
-0.987  0.001\\
%-0.823  0.15\\
%-0.713  0.4\\
%-0.684  0.5\\
%-0.542  0.6\\
...
    };

etc. para fazer um loop nessas imagens. Eu preferiria uma solução automatizada.

Eu usaria a classe beamer e compilaria com XeLaTeX se essa informação fizesse diferença.

Responder1

Uma animação com beamerferramentas integradas:

A macro selecionada foi emprestada dehttps://tex.stackexchange.com/a/199396/36296

\documentclass{beamer}
\usepackage{pgfplots}
\newlength\figureheight 
\newlength\figurewidth 
\setlength{\figurewidth}{8cm}
\setlength{\figureheight}{4.5cm}

% Style to select only points from #1 to #2 (inclusive)
\pgfplotsset{select coords between index/.style 2 args={
    x filter/.code={
        \ifnum\coordindex<#1\def\pgfmathresult{}\fi
        \ifnum\coordindex>#2\def\pgfmathresult{}\fi
    }
}}

\makeatletter
\newcommand{\slide}{\the\beamer@slideinframe}
\makeatother

\begin{document}
\begin{frame}
\animate<1-22>
\transduration<1-22>{0.5}
\begin{tikzpicture}
    \begin{axis}[%
    /pgf/number format/.cd,
            use comma,
            1000 sep={},
    width=0.951\figurewidth,
    height=\figureheight,
    at={(0\figurewidth,0\figureheight)},
    scale only axis,
    xmin=-1,
    xmax=1,
    xlabel style={font=\color{white!15!black}},
    xlabel={xlabel},
    ymin=0,
    ymax=1,
    ylabel style={font=\color{white!15!black}},
    ylabel={ylabel},
    axis background/.style={fill=white}
    ]
    \addplot [color=black, forget plot,select coords between index={0}{\slide}]
      table[row sep=crcr]{%
-1  0\\
-0.987  0.001\\
-0.823  0.15\\
-0.713  0.4\\
-0.684  0.5\\
-0.542  0.6\\
-0.478  0.65\\
-0.3544 0.7\\
-0.254  0.8\\
-0.14   0.9\\
0   1\\
0.057774545 0.9\\
0.15794 0.8\\
0.258105455 0.7\\
0.358270909 0.65\\
0.458436364 0.6\\
0.558601818 0.5\\
0.658767273 0.4\\
0.758932727 0.15\\
0.859098182 0.05\\
0.959263636 0.001\\
1   0\\
    };
    \end{axis}
    \end{tikzpicture}
    \pause[20]
\end{frame}
\end{document}

insira a descrição da imagem aqui

Responder2

Aqui está uma animação de alguns pontos de dados de uma tabela. Para a animação eu uso oanimate-pacote.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{animate}
\usepackage{tikz}

\begin{filecontents}{mydata.data}
    X Y
    0 2
    1 4
    2 2
    3 5
    4 4
    5 1
\end{filecontents}

\begin{document}
    \begin{animateinline}{2}% frames per second
        \multiframe{5}{ix=1+1}{% the +1 is the step size
            \begin{tikzpicture}
                \begin{axis}[
                        xlabel={X-Name},
                        ylabel={Y-Name},
                        xmin=0, xmax=5,
                        ymin=1, ymax=5,
                        ]
                    \addplot[restrict x to domain=0:\ix,red] table [x index=0,y index=1]{mydata.data};
                \end{axis}
            \end{tikzpicture}
        }
    \end{animateinline}
\end{document}

O resultado é (visualização inicial e final da animação): insira a descrição da imagem aqui

Requisitos para animar:

  • pdfTEX, versão >= 1.20 ou LuaTEX para saída direta em PDF
  • Ghostscript, versão >= 9.15 ou Adobe Distiller para conversão de PS para PDF
  • dvipdfmx, versão >= 20080607 para conversão de DVI para PDF
  • Acrobat Reader (versão >= 7), PDF-XChange, Foxit Reader

Editar: Para repetir as imagens posteriormente, você pode definir a opção looppara animate-package: \usepackage[loop]{animate}.

Responder3

Como o OP usa o padrãoarticleclass que não fornece os recursos de stepping do beamer, este exemplo faz uso do animatepacote para criar o gráfico animado em uma única página do documento (usando o código do samcarter com pequenas modificações):

\documentclass{article}

\usepackage{pgfplots}
\usepackage{animate}

\newlength\figureheight 
\newlength\figurewidth 
\setlength{\figurewidth}{8cm}
\setlength{\figureheight}{4.5cm}

% Style to select only points from #1 to #2 (inclusive)
\pgfplotsset{select coords between index/.style 2 args={
    x filter/.code={
        \ifnum\coordindex<#1\def\pgfmathresult{}\fi
        \ifnum\coordindex>#2\def\pgfmathresult{}\fi
    }
}}

\begin{document}
  \begin{animateinline}[controls]{2} % 2 frames per sec
    \multiframe{22}{idx=1+1}{  % 22 frames for 22 line segments
      \begin{tikzpicture}      % idx=1, 2, ..., 22
        \begin{axis}[%
          /pgf/number format/.cd,
                  use comma,
                  1000 sep={},
          width=0.951\figurewidth,
          height=\figureheight,
          at={(0\figurewidth,0\figureheight)},
          scale only axis,
          xmin=-1,
          xmax=1,
          xlabel style={font=\color{white!15!black}},
          xlabel={xlabel},
          ymin=-0.1,
          ymax=1,
          ylabel style={font=\color{white!15!black}},
          ylabel={ylabel},
          axis background/.style={fill=white}
          ]
          \addplot [
            color=black, forget plot,select coords between index={0}{\idx},
            unbounded coords=jump
          ] table[row sep=crcr]{%
              -1  0\\
              -0.987  0.001\\
              -0.823  0.15\\
              -0.713  0.4\\
              -0.684  0.5\\
              -0.542  0.6\\
              -0.478  0.65\\
              -0.3544 0.7\\
              -0.254  0.8\\
              -0.14   0.9\\
              0   1\\
              0.057774545 0.9\\
              0.15794 0.8\\
              0.258105455 0.7\\
              0.358270909 0.65\\
              0.458436364 0.6\\
              0.558601818 0.5\\
              0.658767273 0.4\\
              0.758932727 0.15\\
              0.859098182 0.05\\
              0.959263636 0.001\\
              1   0\\
          };
        \end{axis}
      \end{tikzpicture}
    }  
  \end{animateinline}
\end{document}

informação relacionada