这是我的代码:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz,tikz-3dplot}
\begin{document}
\begin{tikzpicture}[thick]
\draw[lightgray, dotted, xstep=pi/6, ystep=1] (0,0) grid (2*pi,2);
\draw[gray] (0,0)--(2*pi,0) node[above] {$x$};
\draw[gray] (0,0)--(0,2) node[left] {$y$};
\draw[domain=0:2*pi,samples=100] plot (\x,{1+sin(\x r)});
\foreach \ang in {0,30,60,...,361} { \draw node at (\ang /180*pi ,{1+sin(\ang )}) {\textcolor{red}{$\bullet$}};}
\end{tikzpicture}
\begin{tikzpicture}[thick]
\foreach \ang in {0,...,15} { \draw [lightgray] (0,0) -- (\ang * 180 / 8:2.1);}
\foreach \s in {1, 1.5, 2} {\draw [lightgray] (0,0) circle (\s );}
\draw[domain=0:2*pi,samples=100] plot ({deg(\x)}:{1+sin(\x r)});
\foreach \ang in {0,...,15} { \draw node at (\ang * 180 / 8:{1+sin(\ang * 180 / 8 )}) {\textcolor{red}{$\bullet$}};}
\end{tikzpicture}
\end{document}
我想让每个项目符号都呈现彩虹色,这样左边的图形就与右边的图形颜色协调了。
答案1
我对这个问题的解释有点不同:循环点的颜色,让左图中某个角度的颜色与右图中该角度的颜色相对应。
为此,我编写了一个小型颜色循环命令,该命令遍历预定义的颜色列表。它使用两个计数器,一个用于当前颜色索引的全局计数器和一个循环中的本地计数器。它还使用布尔值来检查循环结束时是否分配了颜色,如果没有,则全局计数器大于列表的长度,应将其重置为 0。
\foreach
下一个颜色在 主体开始时计算tikzpictures
,并且全局计数器在主体结束时增加\foreach
。
MWE(请注意,我减少了极坐标图中的点数以匹配角度):
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz,tikz-3dplot}
\newcounter{clridx}
\newcounter{cllistloop}
\newif\ifclrfound
\makeatletter
\newcommand{\getnextcolor}{%
\setcounter{cllistloop}{0}
\clrfoundfalse
% loop list from 0 until list index = current color index
% at this index define \currcolor and set found to true
\@for\next:=\plotcolors\do{\ifnum\value{cllistloop}=\value{clridx}\xdef\currcolor{\next}\clrfoundtrue\fi\stepcounter{cllistloop}}
% if nothing found: cycle current color to 0 and call lookup function again
\ifclrfound\relax\else\setcounter{clridx}{0}\getnextcolor\fi
}
\makeatother
\begin{document}
% cycle list for colors
\def\plotcolors{red,yellow,green,cyan,blue,violet}
% current color index
\setcounter{clridx}{0}
\begin{tikzpicture}[thick]
\draw[lightgray, dotted, xstep=pi/6, ystep=1] (0,0) grid (2*pi,2);
\draw[gray] (0,0)--(2*pi,0) node[above] {$x$};
\draw[gray] (0,0)--(0,2) node[left] {$y$};
\draw[domain=0:2*pi,samples=100] plot (\x,{1+sin(\x r)});
\foreach \ang in {0,30,60,...,361} { \getnextcolor\filldraw[\currcolor] (\ang /180*pi ,{1+sin(\ang )}) circle (2pt);\stepcounter{clridx}}
\end{tikzpicture}
\begin{tikzpicture}[thick]
% reset color index for new plot
\setcounter{clridx}{0}
\foreach \ang in {0,...,12} { \draw [lightgray] (0,0) -- (\ang * 180 / 6:2.1);}
\foreach \s in {1, 1.5, 2} {\draw [lightgray] (0,0) circle (\s );}
\draw[domain=0:2*pi,samples=100] plot ({deg(\x)}:{1+sin(\x r)});
\foreach \ang in {0,...,12} { \getnextcolor\filldraw[\currcolor] (\ang * 180 / 6:{1+sin(\ang * 180 / 6 )}) circle (2pt);\stepcounter{clridx}}
\end{tikzpicture}
\end{document}
结果:
答案2
这样怎么样?我稍微清理一下你的代码,然后使用\pgfdeclareverticalshading{rainbow}
使用最新的 PGF 手册(第 1202 页)
还有其他类型的阴影:\pgfdeclarehorizontalshading
,,\pgfdeclareradialshading
。\pgfdeclarefunctionalshading
有关更多详细信息,请参阅上述 PGF 手册中的第 114.2 节“声明阴影”。
注意:\shade
和\fill
可以起到同样的效果。我认为这是为了增加 TikZ 的用户友好性而故意为之。
\documentclass{article}
\usepackage{tikz}
\begin{document}
% 2021 pgfmanual.pdf ver 3.1.8b (page 1202)
\pgfdeclareverticalshading{rainbow}{100bp}{
color(0bp)=(red); color(35bp)=(red);
color(45bp)=(yellow); color(55bp)=(green);
color(65bp)=(cyan); color(75bp)=(blue);
color(85bp)=(violet); color(100bp)=(violet)
}
\begin{tikzpicture}[thick]
\draw[lightgray,dotted,xstep=pi/6] (0,0) grid (2*pi,2);
\draw[gray]
(0,0)--(2*pi,0) node[right] {$x$}
(0,0)--(0,2) node[left] {$y$};
\draw[smooth] plot[domain=0:2*pi] (\x,{1+sin(\x r)});
\foreach \ang in {0,30,...,361}
\shade[shading=rainbow] (\ang/180*pi,{1+sin(\ang)}) circle(1mm);
\end{tikzpicture}
\begin{tikzpicture}[thick]
\foreach \ang in {0,...,15}
\draw[lightgray] (0,0) -- (\ang * 180 / 8:2.1);
\foreach \s in {1, 1.5, 2}
\draw [lightgray] (0,0) circle (\s);
\draw[smooth] plot[domain=0:2*pi] ({deg(\x)}:{1+sin(\x r)});
\foreach \ang in {0,...,15}
\shade[shading=rainbow] (\ang*180/8:{1+sin(\ang*180/8)}) circle(1mm);
\end{tikzpicture}
\end{document}