我正在尝试使用以下 MWE 创建一个简单的 3D 图形:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=9cm, height=9cm,
axis lines=center,
z buffer=sort,
xlabel=$x$,
ylabel=$y$,
zlabel=$z$,
every axis x label/.style={at={(ticklabel* cs:1.05)}},
every axis y label/.style={at={(ticklabel* cs:1.075)}},
every axis z label/.style={at={(ticklabel* cs:1.05)}},
axis line style={stealth-stealth, thick},
xmin=-4, xmax=4,
ymin=-4, ymax=4,
zmin=-4, zmax=4,
xtick=\empty,
ytick=\empty,
ztick=\empty,
]
\addplot3[surf, fill=blue!5, opacity=0.5, domain=-4:4, samples=5] {0};
\draw[dashed, thick] (axis cs:-4,-4,-4) -- (axis cs:4,4,4);
\draw[-stealth, ultra thick, red] (axis cs:0,0,0) -- (axis cs:2,2,2);
\end{axis}
\end{tikzpicture}
\end{document}
我得到的结果如下:
我希望 x 轴的正部分显示在平面的前面,而不是像现在这样显示在平面的后面,左侧的虚线显示在平面的后面(通常是靠近“相机”的部分,以隐藏远离相机的东西)。使用axis on top
不会产生我想要的结果。
我将非常感激您的任何建议。
答案1
PGFPlots 没有任何 z 缓冲区延伸到单个图本身之外。要获得所需的结果,您需要按正确的顺序绘制事物:
\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=9cm, height=9cm,
axis lines=center,
xlabel=$x$,
ylabel=$y$,
zlabel=$z$,
every axis x label/.style={at={(ticklabel* cs:1.05)}},
every axis y label/.style={at={(ticklabel* cs:1.075)}},
every axis z label/.style={at={(ticklabel* cs:1.05)}},
axis line style={stealth-stealth, thick},
xmin=-4, xmax=4,
ymin=-4, ymax=4,
zmin=-4, zmax=4,
xtick=\empty, ytick=\empty,ztick=\empty,
]
\draw[dashed, thick] (-4,-4,-4) -- (0,0,0);
\addplot3[surf, fill=blue!5, opacity=0.5, domain=-4:4, samples=5] {0};
\draw[dashed, thick] (0,0,0) -- (4,4,4);
\draw[-stealth, ultra thick, red] (0,0,0) -- (2,2,2);
\draw[-stealth] (0,0,0) -- (0,0,4);
\end{axis}
\end{tikzpicture}
\end{document}
答案2
像这样?
\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{60}{120}
\begin{tikzpicture}[tdplot_main_coords,declare function={a = 3;}]
\draw[<-> , thick,red] (0,0,-2) -- (0,0,2) node[right]{$ z $};
\draw[<->,thick,red] (-2,0,0) -- (2,0,0) node[left]{$ x $};
\draw[<->,thick, red] (0,-2,0) -- (0,2,0) node[right]{$ y $}
;
\begin{scope}[canvas is xy plane at z=0]
\draw [blue!30] (-a,-a) grid (a,a);
\end{scope}
\draw[-stealth, ultra thick, red] (0,0,0) -- (2,2,2);
\draw[dashed] (2,2,2) -- (2,2,0) --(2,0,0)
(2,2,0) -- (0,2,0)
(2,2,2) -- (0,0,2)
(0,0,0) -- (2,2,0)
;
\end{tikzpicture}
\end{document}