![foreach 中的 PGFplots 生成多个图形](https://linux22.com/image/431416/foreach%20%E4%B8%AD%E7%9A%84%20PGFplots%20%E7%94%9F%E6%88%90%E5%A4%9A%E4%B8%AA%E5%9B%BE%E5%BD%A2.png)
我使用 foreach 命令来生成多个图形。我的代码如下:
\documentclass[border=5mm,tikz]{standalone}
\usepackage[portuguese]{babel}
\usepackage[utf8]{inputenc}
\usepackage{tikz,pgfplots}
\usepackage{amsmath}
\begin{document}
\def\r{1}
\foreach \b in {0,0.5,...,6.5}{
\begin{tikzpicture}[xscale=1.4,yscale=1.4]
\begin{axis}[xmin = -1, xmax = 14,
ymin = -0.5, ymax = 3,
axis lines=middle,
axis equal image,
grid,
xtick distance = 2,
ytick distance = 1,
xlabel=$x$,
ylabel=$y$,
samples=201,
every axis x label/.style={
at={(ticklabel* cs:1.03)}},
every axis y label/.style={
at={(ticklabel* cs:1.1)}},
]
\addplot[thick,domain=0:2*pi] ({\b+\r*cos(deg(x))},{\r+\r*sin(deg(x))});
\addplot[blue,domain=0:\b,thick,densely dotted] ({\r*x - \r*sin(deg(x))},{\r*1-\r*cos(deg(x))});
\addplot[mark=*,mark size=1.5pt,blue] coordinates { ( {\r*(\b-sin(deg(\b)))} , {\r*(1-cos(deg(\b)))} ) };
\addplot[mark=*,mark size=1.5pt,red] coordinates {(\b,\r)};
\end{axis}
\end{tikzpicture}
}
\end{document}
我的目的是为每个 \b 值生成一个新的不同图形(独立)。
但是,我注意到 PGFplots 和 \foreach 不起作用。有人能帮帮我吗?
答案1
使用 没有问题foreach
。问题是 对于\b=0
第二个域来说\addplot
没有意义。
\documentclass[border=5mm,tikz]{standalone}
%\usepackage[portuguese]{babel}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepackage{amsmath}
\begin{document}
\def\r{1}
\foreach \b in {0,0.5,...,6.5}
{\begin{tikzpicture}%[xscale=1.4,yscale=1.4]
\begin{axis}[xscale=1.4,yscale=1.4,
xmin = -1, xmax = 14,
ymin = -0.5, ymax = 3,
axis lines=middle,
axis equal image,
grid,
xtick distance = 2,
ytick distance = 1,
xlabel=$x$,
ylabel=$y$,
samples=201,
every axis x label/.style={
at={(ticklabel* cs:1.03)}},
every axis y label/.style={
at={(ticklabel* cs:1.1)}},
]
\addplot[thick,domain=0:2*pi] ({\b+\r*cos(deg(x))},{\r+\r*sin(deg(x))});
\ifdim\b pt>0pt
\addplot[blue,domain=0:\b,thick,densely dotted] ({\r*x - \r*sin(deg(x))},
{\r*1-\r*cos(deg(x))});
\fi
\addplot[mark=*,mark size=1.5pt,blue] coordinates { ( {\r*(\b-sin(deg(\b)))} , {\r*(1-cos(deg(\b)))} ) };
\addplot[mark=*,mark size=1.5pt,red] coordinates {(\b,\r)};
\end{axis}
\end{tikzpicture}
}
\end{document}