
我有一張包含數據點的 tikz 圖片(僅顯示一部分)
\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}
數據是根據測量生成的,不能(輕鬆)用單一函數擬合,或至少我想避免這種情況。
有沒有一種好的方法可以逐步或以 n 點為單位對資料進行動畫(線圖)?
我想避免手動生成
圖片-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\\
...
};
圖片-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\\
...
};
等等以便循環這些圖像。我更喜歡自動化的解決方案。
如果此資訊有所不同,我將使用 beamer 類別並使用 XeLaTeX 進行編譯。
答案1
beamer
帶有板載工具的動畫:
select 巨集是藉來的https://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}
答案2
這是表格中一些數據點的動畫。對於動畫我使用animate
-包裹。
\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}
動畫要求:
- pdfTEX,版本 >= 1.20 或 LuaTEX 用於直接 PDF 輸出
- Ghostscript,版本 >= 9.15 或 Adobe Distiller,用於 PS 到 PDF 轉換
- dvipdfmx,版本 >= 20080607 用於 DVI 到 PDF 轉換
- Acrobat Reader(版本 >= 7)、PDF-XChange、Foxit Reader
編輯:
若要隨後循環影像,您可以設定loop
-package animate
:選項\usepackage[loop]{animate}
。
答案3
由於OP使用標準article
類別不提供 的步驟功能beamer
,此範例利用該animate
套件在單一文件頁面上建立動畫圖形(使用 samcarter 的程式碼並稍加修改):
\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}