我試著畫一條與 3D 圖形一起移動的線來表示積分區域。
因此,底部的黑線代表整合區域的截止點。我想要沿著函數表面移動的線的痕跡。這是 MWE:
\PassOptionsToPackage{usenames,dvipsnames,table,x11names}{xcolor}
\documentclass[a4paper, 12pt]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps,fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
zmax=15,
zmin=0,
view = {45}{45},
grid=minor,
colormap={mycol}{color=(Tan), color=(Tan)},
xlabel = $s$,
ylabel = $h$,
zlabel = {$f(s,h)$},
ticks = none,
]
\addplot3[
surf,
samples=30,
domain=0:1.5,
opacity=0.5,
]
{12*exp(-(4*x+3*y))};
\draw[black, thick] (0,0,0) -- (1.5,1.5,0);
\addplot3 [name path = xline, draw = none, domain=0:1.5] (x,0,0);
\addplot3 [name path = xcurve, domain=0:1.5, y domain = 0:0, draw = none]
(x, 0, {12*exp(-(4*x))});
\addplot [color = Tan, opacity = 0.5, draw = none]
fill between[of = xcurve and xline];
\addplot3[
mesh,
draw=Bittersweet,
samples=30,
domain=0:1.5,
opacity = 0.75
]
{12*exp(-(4*x+3*y))};
% Attempt 1
%\addplot3 [domain=0:1.5, black, thick, samples=30] (x,x,{12*exp(-(4*x+3*y))});
%Attempt 2
%\addplot3 [domain=0:1.5, black, thick, samples=30] (x,x,{12*exp(-(7*x))});
\end{axis}
\end{tikzpicture}
最後註解掉的兩行,我稱之為嘗試 1 和嘗試 2,就是我這樣做的兩次嘗試。以下是他們每個人的結果:
嘗試1
嘗試2
嘗試 1 很混亂,但嘗試 2 非常接近我想要的,但它在函數的起點和終點之間畫了一條線。關於如何解決這個問題有什麼建議嗎?
答案1
你已經走在正確的軌道上了。您可以透過新增samples y=1
第二次嘗試來實現您想要的目標。
(此外,我對您的程式碼做了一些小的優化。有關詳細信息,請參閱程式碼中的註釋。)
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{
compat=1.16,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
zmax=15,
zmin=0,
view={45}{45},
grid=minor,
colormap={mycol}{color=(Tan), color=(Tan)},
xlabel=$s$,
ylabel=$h$,
zlabel={$f(s,h)$},
ticks=none,
% (moved common options here)
domain=0:1.5,
samples=30,
]
\addplot3[
surf,
opacity=0.5,
% removed one `\addplot' by adding the next line
faceted color=Bittersweet,
] {12*exp(-(4*x+3*y))};
\draw [black, thick] (0,0,0) -- (1.5,1.5,0);
\addplot3 [
name path=xline,
draw=none,
] (x,0,0);
\addplot3 [
name path=xcurve,
% replaced this ...
% y domain=0:0,
% by ...
samples y=1,
draw=none,
] (x,0,{12*exp(-(4*x))});
\addplot [color=Tan, opacity=0.5]
fill between [of=xcurve and xline];
% Attempt 2
\addplot3 [
black,
thick,
samples y=1, % <-- added
] (x,x,{12*exp(-7*x)});
\end{axis}
\end{tikzpicture}
\end{document}