pgfplots:如何在循环中覆盖宏来计算域值

pgfplots:如何在循环中覆盖宏来计算域值

我想循环绘制多个圆弧(百分比)。下一个圆弧应与上一个圆弧相连:
50% (Start=0, End=180°), then 25% (Start=180°, End=180°+90°),....

我该如何使用循环正确地完成此操作?

在此处输入图片描述

\documentclass[border=5pt, tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}

\begin{tikzpicture}[every path/.style={very thick},
%declare function={Start(\x)=(\Start)*3.6;
%End(\x)=(Start(\x)+\x)*3.6+1;}
]
% Start Values
\pgfmathsetmacro\Start{0}
\pgfmathsetmacro\End{0}

\begin{axis}[axis lines=middle, axis equal, title=Actual]
\foreach \Percent/\Color in {50/blue, 25/red}{

\pgfmathsetmacro\Start{\End}
\pgfmathsetmacro\End{(\Start+\Percent)*3.6}

\edef\temp{%
\noexpand\addplot[domain=\Start:\End, draw=\Color]({2*cos(x)}, {2*sin(x)})  coordinate[pos=0, label=S\Start](liStart) coordinate[label=E\End](liEnd);
}\temp}
 \end{axis}
 \end{tikzpicture}

\begin{tikzpicture}[every path/.style={very thick},]
\begin{axis}[axis lines=middle, axis equal, title=Target]
\addplot[domain=0:180, draw=blue]({2*cos(x)}, {2*sin(x)});
\addplot[domain=180:270, draw=red]({2*cos(x)}, {2*sin(x)});
\end{axis}
\end{tikzpicture}

\end{document}

答案1

您需要\pgfplotsforeachungrouped,并且存在一个逻辑问题:

\pgfmathsetmacro\End{(\Start+\Percent)*3.6}

是错误的,因为它会倍增一切3.6你只想繁殖\Percent

\documentclass[border=5pt, tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}

\begin{tikzpicture}[every path/.style={very thick},
%declare function={Start(\x)=(\Start)*3.6;
%End(\x)=(Start(\x)+\x)*3.6+1;}
]
% Start Values
\pgfmathsetmacro\Start{0}
\pgfmathsetmacro\End{0}

\begin{axis}[axis lines=middle, axis equal, title=Actual]
\pgfplotsforeachungrouped \Percent/\Color in {50/blue, 25/red}{

\pgfmathsetmacro\Start{\End}
\pgfmathsetmacro\End{\Start+\Percent*3.6}
\edef\temp{%
\noexpand\addplot[domain=\Start:\End, draw=\Color,smooth]({2*cos(x)}, {2*sin(x)})  coordinate[pos=0, label=S\Start](liStart) coordinate[label=E\End](liEnd);
}\temp}
 \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

顺便说一句,我并没有尝试通过避免剪辑来使输出更漂亮。

相关内容