tikz-pgf – How to Make a Longitudinal Sine Wave Shading with Pgfplots

colorpgfplotsshadingtikz-pgf

At the moment I use TikZ shadings to show up longitudinal waves in my document like this:

sine wave

The shading is white where the sine is –1 and blue where the sine equals 1. This looks ok, but unfortunately it is in RGB and I need a clean CMYK document. The above mentioned questions led me to use pgfplots to make the shading but actually I don’t know how …

This is my 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}

It would be great if I can only specify the function, e.g. \A*cos(2*pi/\T*x) and a rectangle to clip the shading, and the rest of the work is done by TeX …


Bonus question: How can I imitate a radial shading like this one

radial shading

which is done with

\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}

Best Answer

Here is an attempt to replicate the same dimensions with pgfplots:

enter image description here

\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}

The idea is to use a surf plot which is viewed from top. The color data is the value of f(x,y), i.e. the cos value, mapped linearly into the colormap. The smallest color is "white" and the largest is "wave". The shading is generated in CMYK as is the color interpolation.


A radial shading can be done using data cs=polar (which means x= angle, y =radius):

\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}

enter image description here