我试图在 pgfplots 中的 3d surf 图上叠加 2d 线,其中 2d 线是函数 z=(1/2)* (x^2)*(y^2) 的最小值,如下图所示。是否可以将线在视图中时分层到 3d 图的“顶部”,不在视图中时分层到“后面”?我只能通过将 2d 线的范围限制在它们应该“在视图中”时人为地做到这一点,这是一个不完美的解决方案,因为 2d 图中方形边缘与 3d 图的曲率不匹配,而且非常繁琐。
以下代码:
\documentclass[tikz, convert={outext=.png}]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat = newest}
\begin{document}
\pgfplotsset{
colormap/viridis,
}
\begin{tikzpicture}[baseline]
\begin{axis}[xmin=-2, xmax=2, ymin=-2, ymax=2, xlabel = $x$, ylabel = $y$]
\addplot3[
samples = 50,
samples y = 50,
domain = -2:2,
domain y = -2:2,
surf,
shader=interp,
]
{0.5*x^2*y^2};
\addlegendentry{$\frac{1}{2} x^2 y^2 $}
\addplot3[line width = 2pt, color=red, variable=\t, samples y=0, domain=-2:2] ({0}, {t}, {0});
\addlegendentry{$x=0$}
\addplot3[line width = 2pt, color=orange, variable=\t, samples y=0, domain=-2:2] ({t}, {0}, {0});
\addlegendentry{$y=0$}
\end{axis}
\end{tikzpicture}
\end{document}
感谢您的帮助!
答案1
这是一个使用类似想法的解决方法:将表面和红线分成两部分绘制,然后对它们进行排序。我也创建了一些样式,这样我就避免重复一些代码:
\documentclass[tikz, convert={outext=.png}]{standalone}
\usepackage{pgfplots}
\pgfplotsset
{
compat = newest,
colormap/viridis,
my line/.style={line width=2pt, line cap=rect, color=#1},
my surf/.style=
{
samples = 50,
samples y = 50,
domain = -2:2,
domain y = #1,
surf,
shader=interp,
}
}
\begin{document}
\begin{tikzpicture}[baseline]
\begin{axis}[xmin=-2, xmax=2, ymin=-2, ymax=2, xlabel=$x$, ylabel=$y$]
\addplot3[my surf=-0.5:2]{0.5*x^2*y^2};
\addlegendentry{$\frac{1}{2} x^2 y^2 $}
\addplot3[my line=red] coordinates {(0,-0.5,0) (0,2,0)};
\addlegendentry{$x=0$}
\addplot3[my line=orange] coordinates {(-2,0,0) (2,0,0)};
\addlegendentry{$y=0$}
\addplot3[my surf=-2:-0.5]{0.5*x^2*y^2};
\addplot3[my line=red] coordinates {(0,-2,0) (0,-0.5,0)};
\end{axis}
\end{tikzpicture}
\end{document}