填滿 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}

結果如下:

在此輸入影像描述

現在,這是 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}

這是結果:

在此輸入影像描述

任何人都可以對發生的事情提出任何建議嗎?看來當我透過向同一個圖添加多個函數時\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}

這是結果:

在此輸入影像描述

我現在的問題是,為什麼原來的函數要上移?

另一個編輯

我已經找到了一種方法來獲得我想要的東西(不包括下面的優秀答案),但我仍然有一些擔憂。這是一個具有良好結果的 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}

這給出了這個結果:

在此輸入影像描述

現在,問題是線路%shader=interp,。當我取消註釋時,會發生這種情況:

在此輸入影像描述

那麼這個指令是否會因為某些原因強迫圖形向上移動呢?為什麼會出現這種情況?

答案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}

在此輸入影像描述

或者,如果你用

\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;

你得到

在此輸入影像描述

請注意,我添加了\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;

相關內容