如何使用 pgfplots 制作纵向正弦波阴影?

如何使用 pgfplots 制作纵向正弦波阴影?

目前,我使用 TikZ 阴影在我的文档中显示纵波,如下所示:

正弦波

正弦值为 -1 时阴影为白色,正弦值为 1 时阴影为蓝色。这看起来还不错,但不幸的是它是 RGB 格式的,我需要一个干净的CMYK文档。上述问题让我使用来pgfplots制作阴影,但实际上我不知道如何……

这是我的 MWE

\documentclass{article}

\usepackage[cmyk]{xcolor}
    \definecolor{wave}{cmyk}{1,0.35,0,0}
\usepackage{tikz}
    \usetikzlibrary{calc}
    \tikzset{
        samples=100,
    }
\usepackage{pgfplots}
    \pgfplotsset{compat=1.11}

% Variables
\pgfmathsetmacro\T{1}
\pgfmathsetmacro\A{0.2}
\pgfmathsetmacro\N{5}
\pgfmathsetmacro\D{\N*\T}

\begin{document}
\section{With TikZ}
\begin{tikzpicture}
    % Shading
    \coordinate (C) at (0.25pt,0);% small overlapping
    \foreach \x in {1,...,\N} {
        \shade [shading=axis, right color=white, left color=wave, shading angle=90]
            ($(\x*\T-\T,-\A)-(C)$) rectangle ++($(\T/2,2*\A)+(C)$);
        \shade [shading=axis, left color=white, right color=wave, shading angle=90]
            ($(\x*\T-\T/2,-\A)-(C)$) rectangle ++($(\T/2,2*\A)+(C)$);
    }
    % Cosine Wave
    \draw [black] plot [id=sine, domain=0:\D]
        function {\A*cos(2*pi/\T*x)};
\end{tikzpicture}

\section{With PGFplots}
\begin{tikzpicture}
    \begin{axis}
    \addplot [domain=0:\D] {\A*cos(2*pi/\T*x)};
    \end{axis}
\end{tikzpicture}
\end{document}

如果我只能指定函数(例如,\A*cos(2*pi/\T*x)一个矩形来裁剪阴影),其余工作由 TeX 完成,那就太好了……


附加问题:我怎样才能模仿这样的径向阴影

放射状阴影

完成

\pgfdeclareradialshading{wave}{\pgfpoint{0cm}{0cm}}%
    {%
        color(0.0cm)=(white);
        color(0.01cm)=(white);
        color(0.1cm)=(wave);
        color(0.2cm)=(white);
        color(0.3cm)=(wave);
        color(0.4cm)=(white);
        color(0.5cm)=(wave);
        color(0.6cm)=(white);
        color(0.7cm)=(wave);
        color(0.8cm)=(white);
        color(0.9cm)=(wave)
    }
\begin{tikzpicture}
        \shade [shading=wave] circle [radius=5] ;
\end{tikzpicture}

答案1

以下是尝试复制相同尺寸的尝试pgfplots

在此处输入图片描述

\documentclass{standalone}

\usepackage[cmyk]{xcolor}
    \definecolor{wave}{cmyk}{1,0.35,0,0}
\usepackage{tikz}
    \usetikzlibrary{calc}
    \tikzset{
        samples=100,
    }
\usepackage{pgfplots}
    \pgfplotsset{compat=1.11}

% Variables
\pgfmathsetmacro\T{1}
\pgfmathsetmacro\A{0.2}
\pgfmathsetmacro\N{5}
\pgfmathsetmacro\D{\N*\T}

\begin{document}
\begin{tikzpicture}
    % Shading
    \coordinate (C) at (0.25pt,0);% small overlapping
    \foreach \x in {1,...,\N} {
        \shade [shading=axis, right color=white, left color=wave, shading angle=90]
            ($(\x*\T-\T,-\A)-(C)$) rectangle ++($(\T/2,2*\A)+(C)$);
        \shade [shading=axis, left color=white, right color=wave, shading angle=90]
            ($(\x*\T-\T/2,-\A)-(C)$) rectangle ++($(\T/2,2*\A)+(C)$);
    }
    % Cosine Wave
    \draw [black] plot [id=sine, domain=0:\D]
        function {\A*cos(2*pi/\T*x)};
\end{tikzpicture}

\begin{tikzpicture}
    \begin{axis}[
        view={0}{90},
        hide axis,
        colormap={custom}{color=(white) color=(wave)},
        trig format plots=rad,
        x=1cm,
        y=1cm,
        z=0cm,
    ]
    \addplot3[
        domain=0:\D,samples=100,
        domain y=-\A:\A,samples y=2,
        surf,shader=interp,
    ] {\A*cos(2*pi/\T * x)};

    \addplot [domain=0:\D,samples=100] {\A*cos(2*pi/\T*x)};
    \end{axis}
\end{tikzpicture}
\end{document}

这个想法是使用surf从顶部查看的图。颜色数据是 的值f(x,y),即 cos 值,线性映射到颜色图中。最小的颜色是“白色”,最大的是“波浪”。阴影在 CMYK 中生成,颜色插值也是如此。


可以使用以下方法进行径向阴影data cs=polar(即 x = 角度,y = 半径):

\documentclass{standalone}

\usepackage[cmyk]{xcolor}
\definecolor{wave}{cmyk}{1,0.35,0,0}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

% Variables
\pgfmathsetmacro\T{1}
\pgfmathsetmacro\A{0.2}
\pgfmathsetmacro\N{5}
\pgfmathsetmacro\D{\N*\T}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        view={0}{90},
        hide axis,
        colormap={custom}{color=(white) color=(wave)},
        trig format plots=rad,
        x=1cm,
        y=1cm,
        z=0cm,
    ]
    \clip (-\D/2,-\D/2) rectangle (\D/2, \D/2);
    \addplot3[
        data cs=polar,
        domain=0:2*pi,
        domain y=0:\D,samples y=25,
        surf,shader=interp,
    ] {\A*sin(2*pi/\T * y)};

    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

如果您希望它更加灵活,您还可以使用 pgfplots 的 gnuplot 版本。

这里是 7 个点源的干涉图案的例子,具有下列参数 * \Lambda 是波长 * \DistanceSources 是源之间的恒定距离 * 振幅可以用函数“amorti”选择,但在距源 \Cutoff 的半径内保持恒定 * \RetardIIvsI 是两个相邻源之间的恒定相移 * \Date 是添加到所有源的恒定相位。

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}[]
   \pgfmathsetmacro{\xmin}{-6}
   \pgfmathsetmacro{\ymin}{-6}
   \pgfmathsetmacro{\xmax}{6}
   \pgfmathsetmacro{\ymax}{6}
   \pgfmathsetmacro{\Lambda}{.5}
   \pgfmathsetmacro{\DistanceSources}{\Lambda/2}
   \pgfmathsetmacro{\CentreI}{-4*\DistanceSources}
   \pgfmathsetmacro{\CentreII}{\CentreI+\DistanceSources}
   \pgfmathsetmacro{\CentreIII}{\CentreII+\DistanceSources}
   \pgfmathsetmacro{\CentreIV}{\CentreIII+\DistanceSources}
   \pgfmathsetmacro{\CentreV}{\CentreIV+\DistanceSources}
   \pgfmathsetmacro{\CentreVI}{\CentreV+\DistanceSources}
   \pgfmathsetmacro{\CentreVII}{\CentreVI+\DistanceSources}
   \pgfmathsetmacro{\Cutoff}{\Lambda/10}
   \pgfmathsetmacro{\RetardIIvsI}{pi/2}
   \pgfmathsetmacro{\Date}{0}

   \begin{axis}[colormap/blackwhite,
      view ={0}{90},
      xlabel = $x$,
      ylabel = $y$,
      extra x ticks = ,
      extra x tick labels = ,
      extra y ticks = ,
      extra y tick labels = ,
      xmin = \xmin,
      ymin = \ymin,
      xmax = \xmax,
      ymax = \ymax,
      domain = \xmin:\xmax,
      samples = 50
      ]
      \addplot3[surf,shader=interp,raw gnuplot] gnuplot {
          set isosamples 50,50;
          amorti (centre,xy,y,cutoff)= 1;
          interf(centre,date,x,y,cutoff,lambda,retard) =%
          amorti(centre,x,y,cutoff)%
          * cos(date-2*pi*sqrt((x-centre)**2 + y**2)/lambda-retard);
          splot [x=\xmin:\xmax] [y=\ymin:\ymax] 1./7.*(%
             interf(\CentreI,\Date,x,y,\Cutoff,\Lambda,0)%
           + interf(\CentreII,\Date,x,y,\Cutoff,\Lambda,\RetardIIvsI)%
           + interf(\CentreIII,\Date,x,y,\Cutoff,\Lambda,2*\RetardIIvsI)%
           + interf(\CentreIV,\Date,x,y,\Cutoff,\Lambda,3*\RetardIIvsI)%
           + interf(\CentreV,\Date,x,y,\Cutoff,\Lambda,4*\RetardIIvsI)%
           + interf(\CentreVI,\Date,x,y,\Cutoff,\Lambda,5*\RetardIIvsI)%
           + interf(\CentreVII,\Date,x,y,\Cutoff,\Lambda,6*\RetardIIvsI)%
           ) 
          };
      \end{axis}
  \end{tikzpicture}
 \end{document}

在此处输入图片描述

相关内容