使用 animate 套件對 pgfplot 圖形進行動畫處理

使用 animate 套件對 pgfplot 圖形進行動畫處理

在谷歌搜尋如何對情節進行動畫處理後,我發現了我沒有得到的不同方法,在使用它之前我已經做了一些動畫方法。我的問題是否可以使用相同的方法來製作以下情節的動畫?

在此輸入影像描述

(我幾天後需要這個動畫,所以我無法學習其他方法)

\documentclass[tikz]{standalone}
\usetikzlibrary{animations}
\usepackage{animate}
\usepackage{ifthen}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{document}

\foreach \x in {0,1,...,20} {
\begin{tikzpicture}
\useasboundingbox (-3,-5) rectangle (9,10);
\begin{axis}[%title={test},
             legend style={font=\fontsize{3}{3}\selectfont},
             legend pos=south east,
             axis lines=center,
             domain=0.2:8,
             xtick={0,1,...,7},
             ytick={0,1,...,4},
             samples=501,
             xlabel={}]
\addplot [gray, dashed] {1};

\addplot [color=red] {1-exp(-\x)*cos(3*deg(\x))};

\end{axis}
\end{tikzpicture}
}
\end{document}

答案1

對於當前的問題,繪圖域的上限需要進行動畫處理。

\documentclass{standalone}
%\documentclass[dvisvgm]{standalone} % latex <file>.tex ; dvisvgm --font-format=woff2 --zoom=-1  --exact <file>.dvi 

\usepackage[T1]{fontenc}
\usepackage{animate}
\usepackage{pgfplots}

\begin{document}

\begin{animateinline}[controls]{20}
\multiframe{81}{rXmax=0+0.1}{
  \begin{tikzpicture}
  \begin{axis}[
    axis lines=center,
    domain=0.001:\rXmax,
    xtick={0,1,...,8},
    xmax=8.4,
    ymax=1.6,
    samples=501
  ]
  \addplot [gray, dashed] {1};
  
  \addplot [color=red] {1-exp(-x)*cos(3*deg(x))};
  
  \end{axis}
  \end{tikzpicture}
}
\end{animateinline}

\end{document}

答案2

我的解決方案使用gnuplot並且必須使用 進行編譯(僅在第一次取得曲線點表時)pdflatex.exe -shell-escape %.tex

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{math,backgrounds}
\begin{document}

\foreach \xDom in {0.2,0.3,...,7.2} {
\begin{tikzpicture}[background rectangle/.style={fill=white},show background rectangle]
\tikzmath{
\samplePoints= int(\xDom*100);
\idFile= int(\xDom*10);
}
\begin{axis}[%title={test},
             legend style={font=\fontsize{3}{3}\selectfont},
             legend pos=south east,
             axis lines=center,
             domain=0.2:\xDom,
             xmin=0.2,xmax=7.5,
             ymin={1-exp(-0.2)*cos(3*deg(0.2))},
             ymax=1.5,
             xtick={0,1,...,7},
             ytick={0,1,...,4},
             samples=\samplePoints,
             xlabel={}]
\draw[gray, dashed] (axis cs:0,1) -- (axis cs:7.2,1);

\addplot [color=red] gnuplot[id=gr\idFile] {1-exp(-x)*cos(3*x)};

\end{axis}
\end{tikzpicture}
}
\end{document}

可以看到,我已經為 tikzpicture 添加了背景,因為我發現透過這種方式轉換為 GIF 效果要好得多(鋸齒較少)。編譯 LaTeX 程式碼後,您必須使用 .txt 檔案將多頁 PDF 轉換為 GIF ImageMagick。我在命令列中使用以下指令:

magick convert -verbose -density 300 -strip -resize 50% -layers OptimizePlus -delay 30 *.pdf myGIF.gif

如果您不使用 Windows,請省略初始的magick

結果是:

在此輸入影像描述

編輯: 正如 AlexG 所說,背景不是必需的,您可以-alpha removeconvert.因此,改進的程式碼(帶有一些其他更改)是:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{math}
\begin{document}

\foreach \xDom [count=\idFile] in {0.2,0.21,...,7.2} {
\begin{tikzpicture}
\tikzmath{
\samplePoints= ifthenelse(\xDom==0.2, 1, int(\xDom*100));
}
\begin{axis}[%title={test},
             legend style={font=\fontsize{3}{3}\selectfont},
             legend pos=south east,
             axis lines=center,
             domain=0.2:\xDom,
             xmin=0.2,xmax=7.5,
             ymin={1-exp(-0.2)*cos(3*deg(0.2))},
             ymax=1.5,
             xtick={0,1,...,7},
             ytick={0,1,...,4},
             samples=\samplePoints,
             xlabel={}]
\draw[gray, dashed] (axis cs:0,1) -- (axis cs:7.2,1);

\addplot [color=red] gnuplot[id=gr\idFile] {1-exp(-x)*cos(3*x)};

\end{axis}
\end{tikzpicture}
}
\end{document}

與之前一樣,必須使用以下命令進行編譯pdflatex.exe -shell-escape %.tex才能取得多頁 PDF,然後使用:

magick convert -verbose -density 300 -strip -resize 40% -alpha remove -layers OptimizePlus -delay 1 *.pdf myGif.gif

結果是:

在此輸入影像描述

相關內容