填充 3D 图区域

填充 3D 图区域

我正在尝试使用来自的想法来填充此 3D 图形下的区域这个帖子下面是我想要绘制的函数的 MWE:

\PassOptionsToPackage{usenames,dvipsnames,table,x11names}{xcolor}
\documentclass[a4paper, 12pt]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps,fillbetween}

\begin{document}

\begin{tikzpicture}
\pgfdeclarelayer{pre main}
\pgfsetlayers{pre main,main}

\begin{axis}[
zmax=1.25,
view = {45}{45},
grid=minor,
colormap={mycol}{color=(SteelBlue3), color=(SteelBlue3)},
xlabel = $x$,
ylabel = $y$,
zlabel = {$f(x,y)$},
]

\addplot3[
surf,
samples=30,
domain=0:2,
shader=interp,
opacity=0.5,
]
{exp(-(x^2+y^2))};

\end{axis}
\end{tikzpicture}

\end{document}

结果如下:

enter image description here

现在,这是 MWE 及其代码应该侧面阴影:

\PassOptionsToPackage{usenames,dvipsnames,table,x11names}{xcolor}
\documentclass[a4paper, 12pt]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps,fillbetween}

\begin{document}

\begin{tikzpicture}
\pgfdeclarelayer{pre main}
\pgfsetlayers{pre main,main}

\begin{axis}[
zmax=1.25,
view = {45}{45},
grid=minor,
colormap={mycol}{color=(SteelBlue3), color=(SteelBlue3)},
xlabel = $x$,
ylabel = $y$,
zlabel = {$f(x,y)$},
]

\begin{pgfonlayer}{pre main}
\addplot3[
surf,
samples=30,
domain=0:2,
shader=interp,
opacity=0.5,
]
{exp(-(x^2+y^2))};
\end{pgfonlayer}

\addplot3 [name path = xline, draw = none] (x,0,0);
 % x is the variable
 % second 0 is the y coordinate of the leftmost part of the graph
 % third 0 is height
\addplot3 [name path = yline, draw = none] (2,y,0);
 % y is the variable
 % first 2 is the x coordinate of the rightmost part of the graph
 % third 0 is height
\addplot3 [name path = xcurve, y domain = 0:0, draw = none] 
    (x, 0, {exp(-x^2)});
 % now instead of height being 0, it is height of function
\addplot3 [name path = ycurve, y domain = 0:0, draw = none]
  (2, x, {exp(-(x^2 + 4))});
 % same idea
\addplot [color = SteelBlue3, opacity = 0.5, draw = none]
  fill between[of = xcurve and xline];
 % fills in x axis
\addplot [color = SteelBlue3, opacity = 0.5, draw = none]
  fill between[of = yline and ycurve, reverse = true];

\end{axis}
\end{tikzpicture}

\end{document}

结果如下:

enter image description here

有人能提出一些建议,看看到底发生了什么吗?似乎当我通过 向同一个图添加多个函数时\addplot3[],一切都变得一团糟。

抱歉,这看起来像是“只需为我解决这个问题”,但我不知道我做错了什么。

编辑

我现在看到,出于某种原因,在侧面阴影中添加新图时,域从 (0,2) 更改为 (-4,4),因此在添加图时设置域会有所帮助:

\PassOptionsToPackage{usenames,dvipsnames,table,x11names}{xcolor}
\documentclass[a4paper, 12pt]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps,fillbetween}

\begin{document}

\begin{tikzpicture}
\pgfdeclarelayer{pre main}
\pgfsetlayers{pre main,main}

\begin{axis}[
zmax=1.25,
view = {45}{45},
grid=minor,
colormap={mycol}{color=(SteelBlue3), color=(SteelBlue3)},
xlabel = $x$,
ylabel = $y$,
zlabel = {$f(x,y)$},
]

\begin{pgfonlayer}{pre main}
\addplot3[
surf,
samples=30,
domain=0:2,
shader=interp,
opacity=0.5,
]
{exp(-(x^2+y^2))};
\end{pgfonlayer}

\addplot3 [name path = xline, draw = none, domain=0:2] (x,0,0);
 % x is the variable
 % second 0 is the y coordinate of the leftmost part of the graph
 % third 0 is height
\addplot3 [name path = yline, draw = none, domain=0:2] (2,y,0);
 % y is the variable
 % first 2 is the x coordinate of the rightmost part of the graph
 % third 0 is height
\addplot3 [name path = xcurve, domain=0:2, y domain = 0:0, draw = none] 
    (x, 0, {exp(-x^2)});
 % now instead of height being 0, it is height of function
\addplot3 [name path = ycurve, domain=0:2, y domain = 0:0, draw = none]
  (2, x, {exp(-(x^2 + 4))});
 % same idea
\addplot [color = SteelBlue3, opacity = 0.5, draw = none]
  fill between[of = xcurve and xline];
 % fills in x axis
\addplot [color = SteelBlue3, opacity = 0.5, draw = none]
  fill between[of = yline and ycurve, reverse = true];

\end{axis}
\end{tikzpicture}

\end{document}

结果如下:

enter image description here

我现在的问题是,为什么原来的功能被上移了?

另一项编辑

我已经找到了一种方法来获得我想要的东西(不包括下面的优秀答案),但我还有一些顾虑。这是一个具有良好结果的 MWE:

\PassOptionsToPackage{usenames,dvipsnames,table,x11names}{xcolor}
\documentclass[a4paper, 12pt]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps,fillbetween}

\begin{document}

\begin{tikzpicture}

\begin{axis}[
zmax=1.25,
zmin=0,
view = {45}{45},
grid=minor,
colormap={mycol}{color=(SteelBlue3), color=(SteelBlue3)},
xlabel = $x$,
ylabel = $y$,
zlabel = {$f(x,y)$},
]

\addplot3[
surf,
samples=30,
domain=0:2,
%shader=interp,
opacity=0.5,
]
{exp(-(x^2+y^2))};

\addplot3 [name path = xline, draw = none, domain=0:2] (x,0,0);
 % x is the variable
 % second 0 is the y coordinate of the leftmost part of the graph
 % third 0 is height
\addplot3 [name path = yline, draw = none, domain=0:2] (2,y,0);
 % y is the variable
 % first 2 is the x coordinate of the rightmost part of the graph
 % third 0 is height
\addplot3 [name path = xcurve, domain=0:2, y domain = 0:0, draw = none] 
    (x, 0, {exp(-x^2)});
 % now instead of height being 0, it is height of function
\addplot3 [name path = ycurve, domain=0:2, y domain = 0:0, draw = none]
  (2, x, {exp(-(x^2 + 4))});
 % same idea
\addplot [color = SteelBlue3, opacity = 0.5, draw = none]
  fill between[of = xcurve and xline];
 % fills in x axis
\addplot [color = SteelBlue3, opacity = 0.5, draw = none]
  fill between[of = yline and ycurve, reverse = true];

\end{axis}
\end{tikzpicture}

\end{document}

得出以下结果:

enter image description here

现在,问题出在这%shader=interp,条线上。当我取消注释时,发生了以下情况:

enter image description here

那么,这个命令是否会因为某种原因强制图形向上移动?为什么会发生这种情况?

答案1

像这样吗?

\PassOptionsToPackage{usenames,dvipsnames,table,x11names}{xcolor}
\documentclass[a4paper, 12pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{colormaps,fillbetween}

\begin{document}

\begin{tikzpicture}[declare function={f(\x,\y)=exp(-(\x*\x+\y*\y));}]
\pgfdeclarelayer{pre main}
\pgfsetlayers{pre main,main}

\begin{axis}[
zmax=1.25,
view = {45}{45},
grid=minor,
colormap={mycol}{color=(SteelBlue3), color=(SteelBlue3)},
xlabel = $x$,
ylabel = $y$,
zlabel = {$f(x,y)$},
]

\addplot3[
surf,
samples=30,
domain=0:2,
shader=interp,
opacity=0.5,
]
{f(x,y)};

\fill[blue] (0,0,-1) -- plot[variable=\x,domain=0:2] (\x,0,{f(\x,0)}) -- (2,0,-1) --
cycle;
\fill[blue!80] (2,0,-1) -- plot[variable=\y,domain=0:2] (2,\y,{f(2,\y)}) 
-- (2,2,-1) --cycle;

\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

或者,如果你用

\fill[SteelBlue3] (0,0,-1) -- plot[variable=\x,domain=0:2] (\x,0,{f(\x,0)}) -- (2,0,-1) --
cycle;
\fill[SteelBlue3] (2,0,-1) -- plot[variable=\y,domain=0:2] (2,\y,{f(2,\y)}) 
-- (2,2,-1) --cycle;

你得到

enter image description here

注意我添加了\pgfplotsset{compat=1.16}。如果你不想这样,你需要在坐标前面加上axis cs:

\fill[SteelBlue3] (axis cs:0,0,-1) -- plot[variable=\x,domain=0:2] (axis cs:\x,0,{f(\x,0)}) -- (axis cs:2,0,-1) --
cycle;
\fill[SteelBlue3] (axis cs:2,0,-1) -- plot[variable=\y,domain=0:2] (axis cs:2,\y,{f(2,\y)}) 
-- (axis cs:2,2,-1) --cycle;

相关内容