2

I have the following three-dimensional surface plot and I would like to add a gradient to the red semitransparent area starting with black at x=0 and ending with white at x=1. Is it possible to add a gradient to the fill between command?

Alternatively, it might be possible to draw the area with the patchplots library. However, I wasn't able to reproduce the area with the patchplots library and the patch type=polygon command, so I wasn't able to check if it is possible to add a gradient this way.

enter image description here

MWE:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        enlargelimits=false,
        axis lines=center,
        height=10cm,
        xmin=0, xmax=1.0,
        ymin=0, ymax=1.0,
        zmin=0, zmax=1.0,
        xtick=\empty, ytick=\empty, ztick=\empty,
        xlabel=$x$, ylabel=$y$, zlabel=$z$,
        axis equal,
        view={130}{25},
        enlargelimits=upper,
        clip=false,
        axis on top,
        set layers,
        ]

\begin{pgfonlayer}{axis background}
\addplot3[
ultra thin,
trig format plots=rad,
surf,
faceted color=black!50,
point meta={sqrt(x^2 +y^2)},
colormap={white}{color=(white) color=(black!15)},
domain=0.001:pi/2,
unbounded coords=jump,
samples=81,
y domain=pi/2:2*pi,
samples y = 51,
z buffer=sort,
] ({sin(x)*cos(y)},{sin(x)*sin(y)},{(sin(10*x) / (10*x))});
\end{pgfonlayer}

\begin{pgfonlayer}{axis foreground}
\addplot3[
very thick,
trig format plots=rad,
draw=red,
domain=0.001:pi/2,
samples=81,
y domain=pi/2:2*pi,
samples y = 1,
name path=A,
] ({sin(x)*cos(y)},{sin(x)*sin(y)},{(sin(10*x) / (10*x))});

\addplot3 [name path=B, draw=none,samples=2, domain=0:1] ({x},0,0);
\addplot[red,opacity=0.4] fill between [of=A and B];
\end{pgfonlayer}

\end{axis}
\end{tikzpicture}
\end{document}
2
  • I can not compile your code. Jul 10, 2021 at 11:07
  • Strange - there was an error: ":53: Package pgfplots Error: Sorry, you can't use 'y' in this context.", but the compilation eventually finished. Jul 10, 2021 at 11:10

1 Answer 1

3
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        enlargelimits=false,
        axis lines=center,
        height=10cm,
        xmin=0, xmax=1.0,
        ymin=0, ymax=1.0,
        zmin=0, zmax=1.0,
        xtick=\empty, ytick=\empty, ztick=\empty,
        xlabel=$x$, ylabel=$y$, zlabel=$z$,
        axis equal,
        view={130}{25},
        enlargelimits=upper,
        clip=false,
        axis on top,
        set layers,
        ]

\begin{pgfonlayer}{axis background}
\addplot3[
ultra thin,
trig format plots=rad,
surf,
faceted color=black!50,
point meta={sqrt(x^2 +y^2)},
colormap={white}{color=(white) color=(black!15)},
domain=0.001:pi/2,
unbounded coords=jump,
samples=81,
y domain=pi/2:2*pi,
samples y = 51,
z buffer=sort,
] ({sin(x)*cos(y)},{sin(x)*sin(y)},{(sin(10*x) / (10*x))});
\end{pgfonlayer}

\begin{pgfonlayer}{axis foreground}
\addplot3[
very thick,
trig format plots=rad,
draw=red,
domain=0.001:pi/2,
samples=81,
samples y = 1,
name path=A,
] ({sin(x)*cos(0)}, {sin(x)*sin(0)}, {(sin(10*x) / (10*x))});

\addplot3 [name path=B, draw=none,samples=2, domain=0:1] ({x},0,0);
\addplot[shade, left color=white, right color=black] fill between [of=A and B];
\end{pgfonlayer}

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

Graph with shaded area

2
  • I corrected the error regarding usage of y in line plot! This is not white at x=1 but at x=pi/2 - I expect that is what is meant. Jul 10, 2021 at 11:30
  • Also technically it is not a gradient along the x axis, but from left to right. I expect that it is not possible to tell the difference. Jul 10, 2021 at 11:33

You must log in to answer this question.

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