双重 foreach 用于颜色变换节点

双重 foreach 用于颜色变换节点

我想要绘制底部的一系列圆圈:

在此处输入图片描述

这是我的代码:

\documentclass[margin=.5cm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[every node/.style={circle}]
 \foreach \a in {100,90, ..., 0}{
 \foreach \n in {1,2, ..., 11}{
  \node [fill={green!\a!red}] at (\n,0)   {};
  }}

  \node [fill={green!100!red}] at (1,-2) {};
  \node [fill={green!90!red}] at (2,-2) {};
  \node [fill={green!80!red}] at (3,-2) {};
  \node [fill={green!70!red}] at (4,-2) {};
  \node [fill={green!60!red}] at (5,-2) {};
  \node [fill={green!50!red}] at (6,-2) {};
  \node [fill={green!40!red}] at (7,-2) {};
  \node [fill={green!30!red}] at (8,-2) {};
  \node [fill={green!20!red}] at (9,-2) {};
  \node [fill={green!10!red}] at (10,-2) {};
  \node [fill={green!0!red}] at (11,-2) {};
\end{tikzpicture}  
\end{document}

为了避免所有重复,我想使用两个foreach循环,但我似乎无法正确使用它们,因为结果是第一组圆圈,没有颜色变化。我做错了什么?

答案1

你不需要两个循环,只需要一个:

在此处输入图片描述

\documentclass[margin=.5cm]{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[every node/.style={circle}]
 \foreach \n in {100,90, ..., 0}{%
    \pgfmathparse{0.1*(100-\n)+1}%
  \node [fill={green!\n!red}] at (\pgfmathresult,0)   {};
  }

  \node [fill={green!100!red}] at (1,-2) {};
  \node [fill={green!90!red}] at (2,-2) {};
  \node [fill={green!80!red}] at (3,-2) {};
  \node [fill={green!70!red}] at (4,-2) {};
  \node [fill={green!60!red}] at (5,-2) {};
  \node [fill={green!50!red}] at (6,-2) {};
  \node [fill={green!40!red}] at (7,-2) {};
  \node [fill={green!30!red}] at (8,-2) {};
  \node [fill={green!20!red}] at (9,-2) {};
  \node [fill={green!10!red}] at (10,-2) {};
  \node [fill={green!0!red}] at (11,-2) {};
\end{tikzpicture}  
\end{document}

相关内容