我想使用交替颜色填充相对于其静态配置振动的圆的波峰和波谷。我尝试使用该fillbetween
包(带有 pgfplots 1.15),它几乎可以正常工作。我应该怎么做才能获得所需的结果?
\documentclass[tikz]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
hide axis,
trig format plots=rad,
view={60}{120}
]
\addplot3 [name path=vibrating, variable=t, domain=-pi:pi, samples=1000, samples y=0] ({cos(t)},{sin(t)},{cos(4*t)});
\addplot3 [name path=flat, variable=t, domain=-pi:pi, samples=100, samples y=0] ({cos(t)},{sin(t)},0);
\addplot3 [fill=gray!5] fill between [
of=vibrating and flat,
split,
every even segment/.style = {red!20!white}];
\tikzfillbetween[
of=vibrating and flat,
on layer=,
split,
every even segment/.style = {fill=none, draw=none}]{blue,opacity=50}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
欢迎来到 TeX.SE!问题是,交叉点始终是 2D 路径(即屏幕上的路径)的交叉点,而不是 3D 交叉点。因此,您图片中使用的交叉点段不符合预期。
因此,可能值得稍微换个角度,用分析方法计算交点,这在给定的设置中足够简单。tikz-3dplot
在这里使用就足够了。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\begin{tikzpicture}[scale=4]
\tdplotsetmaincoords{70}{110}
\pgfmathsetmacro{\Amplitude}{0.3}
\begin{scope}[tdplot_main_coords,samples=101]
\draw plot[variable=\x,domain=0:360] ({cos(\x)},{sin(\x)},{0});
\foreach \X in {1,...,4}
{\draw[fill=red] plot[variable=\x,domain=90*\X+360/16:90*\X+3*360/16,samples=51]
({cos(\x)},{sin(\x)},{\Amplitude*cos(4*\x)})
--plot[variable=\x,domain=90*\X+3*360/16:90*\X+360/16,samples=51]
({cos(\x)},{sin(\x)},{0});
\draw[fill=blue] plot[variable=\x,domain=90*\X-360/16:90*\X+360/16,samples=51]
({cos(\x)},{sin(\x)},{\Amplitude*cos(4*\x)})
--plot[variable=\x,domain=90*\X+360/16:90*\X-360/16,samples=51]
({cos(\x)},{sin(\x)},{0});}
\end{scope}
\end{tikzpicture}
\end{document}
当然,您也可以使用 pgfplots。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
hide axis,
trig format plots=rad,
view={60}{120}
]
\addplot3 [name path=vibrating, variable=t, domain=-pi:pi, samples=1000, samples y=0] ({cos(t)},{sin(t)},{cos(4*t)});
\pgfmathsetmacro{\Amplitude}{1}
\pgfplotsinvokeforeach{1,...,4}{%
\draw[fill=red,fill opacity=0.5] plot[variable=\x,domain=90*#1+360/16:90*#1+3*360/16,samples=51]
({cos(\x)},{sin(\x)},{\Amplitude*cos(4*\x)})
--plot[variable=\x,domain=90*#1+3*360/16:90*#1+360/16,samples=51]
({cos(\x)},{sin(\x)},{0});
\draw[fill=blue,fill opacity=0.5] plot[variable=\x,domain=90*#1-360/16:90*#1+360/16,samples=51]
({cos(\x)},{sin(\x)},{\Amplitude*cos(4*\x)})
--plot[variable=\x,domain=90*#1+360/16:90*#1-360/16,samples=51]
({cos(\x)},{sin(\x)},{0});}
\addplot3 [name path=flat, variable=t, domain=-pi:pi, samples=100, samples y=0] ({cos(t)},{sin(t)},0);
\end{axis}
\end{tikzpicture}
\end{document}
还请注意,对于更复杂的图像,可能需要切换到asymptote
带有真正的 3D 引擎的。