# How to make a longitudinal sine wave shading with pgfplots?

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

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}
\coordinate (C) at (0.25pt,0);% small overlapping
\foreach \x in {1,...,\N} {
($(\x*\T-\T,-\A)-(C)$) rectangle ++($(\T/2,2*\A)+(C)$);
($(\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}
\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

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}
\end{tikzpicture}


Here is an attempt to replicate the same dimensions with 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}
\coordinate (C) at (0.25pt,0);% small overlapping
\foreach \x in {1,...,\N} {
($(\x*\T-\T,-\A)-(C)$) rectangle ++($(\T/2,2*\A)+(C)$);
($(\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)},
x=1cm,
y=1cm,
z=0cm,
]
domain=0:\D,samples=100,
domain y=-\A:\A,samples y=2,
] {\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)},
x=1cm,
y=1cm,
z=0cm,
]
\clip (-\D/2,-\D/2) rectangle (\D/2, \D/2);
data cs=polar,
domain=0:2*pi,
domain y=0:\D,samples y=25,
] {\A*sin(2*pi/\T * y)};

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


• Thank you very much. This is actually, what I tried but I had every plot/.style={smooth} in my document which interfered with the surface plots and made them becoming dimly lines instead … – Tobi Nov 6 '14 at 9:48
• Could you please explain – if possible – how to make a radial shading with this approach (see my edited question)? – Tobi Nov 6 '14 at 10:46
• I have added a solution with radial shadings by means of a surface plot – Christian Feuersänger Nov 6 '14 at 18:37
• @ChristianFeuersänger Perfect... – Enthusiastic Engineer Nov 12 '14 at 8:56

You can also use the gnuplot version of pgfplots if you want it to be more flexible.

Here is an exemple of the interference pattern of 7 point sources with the following parameters * \Lambda is the wavelength * \DistanceSources is the constant distance between the sources * the amplitude can be chosen with the function "amorti" but will be constant within a radius of \Cutoff from the source * \RetardIIvsI is the constant phase shift between two adjacent sources * \Date is the constant phase added to all sources.

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