3

I can, to some extent, define a new color map for a 3d-surface and import some tikz picture as a node inside the 3d-plot. I want to put the 3d-surface over the inserted node object where one of the colors in the colormap should be transparent, while the others just appear as they should over the node object. Clearly opacity option is not what I need. I could do something similar using fading but that was not for plots of functions, but only for simple color gradients. What I have is the following, and I want to make the white transparent to put the surface over the lattice.

\documentclass[10pt,a4paper,notitlepage]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\newsavebox\mybox

\begin{document}
\centering

\savebox\mybox{
\begin{tikzpicture}[scale=1.3]
\fill [green!50] 
    foreach \y in {1,...,10} {
        foreach \x in {1,...,20} {
            ({.25*\x + (-1)^(\y)*0.125*.5}, .25*\y) circle [radius=.10]
        }
    };
\end{tikzpicture}
}
\begin{tikzpicture} [scale=2]
\def\a{0}
\def\b{0}
    \begin{axis} [point meta min=-1., point meta max=1.,
        colormap={emfield}{
        rgb255(0cm)=(0,0,255) rgb255(3cm)=(255,255,255) rgb255(6cm)=(255,0,0)
        },
        axis equal image,
        zmin=-1, zmax=1,
        view={0}{90}, axis lines=none]
        \addplot3 [surf, shader= interp, domain=-4*pi:4*pi, y domain=-2*pi:2*pi, samples=50,
            ] { ((x-\a)^2 + (y-\b)^2)^(-.5)*sin(deg(2*((x-\a)^2 + (y-\b)^2)^(.5))) };
        \node [opacity=1] at (0,0) {\usebox\mybox};
    \end{axis}
\end{tikzpicture}
\end{document}

That looks like

enter image description here

1
  • Maybe you can use tikzfadingfrompicture to create a mask using the plot (but only with black and white) and than apply this mask fading to the green dots ... Commented Sep 10, 2022 at 11:24

2 Answers 2

2

You could make use of fadings and create a fading from your plot using tikzfadingfrompicture which you can then apply to the green dots. This is probably not the most straight-forward approach, but I am unsure whether you can apply a fading to a plot directly.

This only shows the basic approach, because I am usure what the result should look like in the end. Probably, some adjustment of the opacity settings is needed. Also, there may be a more elegant way to achieve proper scaling and placement of the fading.

\documentclass[10pt,a4paper,notitlepage]{article}
\usepackage{tikz}
\usetikzlibrary{fadings}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\newsavebox\mybox

\begin{document}
\centering

\begin{tikzfadingfrompicture} [xscale=.25, yscale=.5, name=plot fading]
\def\a{0}
\def\b{0}
    \begin{axis} [point meta min=-1., point meta max=1.,
        colormap={emfield}{
        rgb255(0cm)=(0,0,0) rgb255(3cm)=(255,255,255) rgb255(6cm)=(0,0,0)
        },
        axis equal image,
        zmin=-1, zmax=1,
        view={0}{90}, axis lines=none]
        \addplot3 [surf, shader=interp, domain=-4*pi:4*pi, y domain=-2*pi:2*pi, samples=50,
            ] { ((x-\a)^2 + (y-\b)^2)^(-.5)*sin(deg(2*((x-\a)^2 + (y-\b)^2)^(.5))) };
    \end{axis}
\end{tikzfadingfrompicture}

\savebox\mybox{
\begin{tikzpicture} [scale=1.3]
\fill [green!50] 
    foreach \y in {1,...,10} {
        foreach \x in {1,...,20} {
            ({.25*\x + (-1)^(\y)*0.125*.5}, .25*\y) circle [radius=.10]
        }
    };
\end{tikzpicture}
}

\begin{tikzpicture} [scale=2]
\def\a{0}
\def\b{0}
    \begin{axis} [point meta min=-1., point meta max=1.,
        colormap={emfield}{
        rgb255(0cm)=(0,0,255) rgb255(3cm)=(255,255,255) rgb255(6cm)=(255,0,0)
        },
        axis equal image,
        zmin=-1, zmax=1,
        view={0}{90}, axis lines=none]
        \addplot3 [surf, shader=interp, domain=-4*pi:4*pi, y domain=-2*pi:2*pi, samples=50,
            ] { ((x-\a)^2 + (y-\b)^2)^(-.5)*sin(deg(2*((x-\a)^2 + (y-\b)^2)^(.5))) };
        \begin{scope}
            \path [scope fading=plot fading] ({-4*pi},{-2*pi}) rectangle ({4*pi},{2*pi});
            \node at (0,0) {\usebox\mybox};
        \end{scope}
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

3
  • 1
    For scaling and placement, you need e.g. \path (0,0) circle[radius=10]; and then fit fading=false. See my answer and also: tex.stackexchange.com/questions/655904/… Commented Sep 10, 2022 at 12:30
  • 1
    Ah, thank you! I was struggling with fit fading=false and in the end decided to just use a scaling that fits. I was wondering whether such a fading could be applied to the plot directly, but it seems that it is not possible, not even if you place the \addplot command in a scope and apply the fading to that. But maybe I am wrong. Commented Sep 10, 2022 at 12:33
  • 1
    I just made the whole axis transparent - it works :o) Commented Sep 10, 2022 at 12:56
2

As @Jasper Habicht writes, you need tikzfadingfrompicture for this. It is not possible to know what result you imagine. I have made the transparency steeper here:

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{fadings}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{tikzfadingfrompicture}[name=myfading]
\begin{axis}[
view={0}{90},
point meta min=-0.5, point meta max=0.5,
colormap={emfield}{color=(transparent!100) color=(transparent!100) color=(transparent!100) color=(transparent!0) color=(transparent!100) color=(transparent!100) color=(transparent!100)},
axis equal image,
axis lines=none,
]
\addplot3 [surf, shader=interp, domain=-4*pi:4*pi, y domain=-2*pi:2*pi, samples=50] { ((x)^2 + (y)^2)^(-.5)*sin(deg(2*((x)^2 + (y)^2)^(.5))) };
\end{axis}
\path (0,0) circle[radius=10]; %encompassing circle for alignment
\end{tikzfadingfrompicture}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
view={0}{90},
point meta min=-1, point meta max=1,
colormap={emfield}{rgb255=(0,0,255) rgb255=(255,255,255) rgb255=(255,0,0)},
axis equal image,
axis lines=none,
]
\addplot3[surf, shader=interp, domain=-4*pi:4*pi, y domain=-2*pi:2*pi, samples=50] { ((x)^2 + (y)^2)^(-.5)*sin(deg(2*((x)^2 + (y)^2)^(.5))) };
\end{axis}
\fill[path fading=myfading, fit fading=false, green!50, scale=1.3] foreach \y in {1,...,10} {foreach \x in {1,...,20} {({.25*\x + (-1)^(\y)*0.125*.5}, .25*\y) circle [radius=.10]}};
\end{tikzpicture}
\end{document}

Code is with samples=50, but image is with samples=80:

Circular pattern with green dots

Edit: By comment from @Jasper Habicht, here is the plot itself made transparent instead of the green dots:

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{fadings}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{tikzfadingfrompicture}[name=myfading]
\begin{axis}[
view={0}{90},
point meta min=-0.5, point meta max=0.5,
colormap={emfield}{color=(transparent!0) color=(transparent!0) color=(transparent!0) color=(transparent!100) color=(transparent!0) color=(transparent!0) color=(transparent!0)},
axis equal image,
axis lines=none,
]
\addplot3 [surf, shader=interp, domain=-4*pi:4*pi, y domain=-2*pi:2*pi, samples=50] { ((x)^2 + (y)^2)^(-.5)*sin(deg(2*((x)^2 + (y)^2)^(.5))) };
\end{axis}
\path (0,0) circle[radius=10]; %encompassing circle for alignment
\end{tikzfadingfrompicture}
\begin{document}
\begin{tikzpicture}
\fill[green!50, scale=1.3] foreach \y in {1,...,10} {foreach \x in {1,...,20} {({.25*\x + (-1)^(\y)*0.125*.5}, .25*\y) circle [radius=.10]}};
\path[scope fading=myfading, fit fading=false] (0,0);
\begin{axis}[
view={0}{90},
point meta min=-1, point meta max=1,
colormap={emfield}{rgb255=(0,0,255) rgb255=(255,255,255) rgb255=(255,0,0)},
axis equal image,
axis lines=none,
]
\addplot3[surf, shader=interp, domain=-4*pi:4*pi, y domain=-2*pi:2*pi, samples=50] { ((x)^2 + (y)^2)^(-.5)*sin(deg(2*((x)^2 + (y)^2)^(.5))) };
\end{axis}
\end{tikzpicture}
\end{document}

Red and blue circles with green dots behind

3
  • capacity exceeded. I still don't know how to fix this for TeXMaker, Windows
    – Physor
    Commented Sep 10, 2022 at 12:26
  • Maybe your computer can not handle samples=80. I will change it back to 50. Commented Sep 10, 2022 at 12:32
  • Thanks a lot. That works for me. Both answers are good but I need more time to look at both of them
    – Physor
    Commented Sep 10, 2022 at 12:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .