18

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

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

  • 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
5

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

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.