我正在尝试使用 tikz 为一条线的多条曲线涂上彩虹色。我在 xcolor 包中找到了颜色的 [wave] 选项,但我无法让它在 tikz 的绘制命令中工作。
\documentclass{article}
\usepackage{tikz}
\usepackage{xcolor}
\begin{document}
\begin{tikzpicture}[scale=0.5]
\draw[help lines] (-9.9,-9.9) grid (9.9,9.9);
\draw[very thick,->] (-10,0) -- (10,0) node[above]{$x$};
\draw[very thick,->] (0,-10) -- (0,10) node[above]{$y$};
\foreach \x in {-9,...,9} \draw[shift={(\x,0)},scale=2] (0pt,2pt) -- (0pt,-2pt);
\foreach \y in {-9,...,9} \draw[shift={(0,\y)},scale=2] (2pt,0pt) -- (-2pt,0pt);
\foreach \k in {1,...,10} %\pgfmathsetmacro{\faktor}{(\k-1)*40+380}
\draw[very thick] (0,11-\k) -- (\k,0);
\end{tikzpicture}
\end{document}
我试过做类似的事情
\draw[color[wave]=\factor]
但它不起作用。
你有什么想法?
答案1
好的,我做到了。
\documentclass{article}
\usepackage[rgb]{xcolor}
\usepackage{tikz}
\begin{document}
\pgfmathsetmacro{\n}{20}
\begin{tikzpicture}[scale=5/\n]
\draw[help lines,step=\n/10] (-\n+0.1,-\n+0.1) grid (\n-0.1,\n-0.1);
\draw[very thick,->] (-\n,0) -- (\n,0) node[above]{$x$};
\draw[very thick,->] (0,-\n) -- (0,\n) node[above]{$y$};
\foreach \x in {-\n,...,\n} \draw[shift={(\x,0)},scale=\n/5] (0pt,2pt) -- (0pt,-2pt);
\foreach \y in {-\n,...,\n} \draw[shift={(0,\y)},scale=\n/5] (2pt,0pt) -- (-2pt,0pt);
\foreach \k in {1,...,\n}{ \pgfmathsetmacro{\faktor}{(\k-1)*400/\n+380}
\definecolor{rainbow}{wave}{\faktor}
\draw[very thick, color=rainbow] (0,\n+1-\k) -- (\k,0);
%\draw[very thick] (0,11-\k) -- (-\k,0);
%\draw[very thick] (0,11-\k) -- (\k,0);
}
\end{tikzpicture}
\end{document}
我定义了makros作为nm的颜色值,并在波系统中定义了一种新颜色。我还添加了一个计数器来记录绘制的线条数,并将间隔移至可见光的间隔(380nm,780nm)。
[rgb]
显然,如果你不添加可选参数,它就不起作用\usepackage[rgb]{xcolor]