我想在代码中将轴标签 x、y 和 z 放在每个轴的顶部。但是,我遇到了 x 和 y 标签未完全显示的问题。下面是我的代码和输出,问题以红色突出显示。您能帮我理解为什么 x 和 y 标签没有完全显示吗?
代码:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = center,
ticks=none,
xmin=-2, xmax=2,
ymin=-2, ymax=2,
zmin=0, zmax=2,
view={135}{25},
]
%% adding axis labels
\node at (axis cs:2.1,0,0) {$x$};
\node at (axis cs:0,2.1,0) {$y$};
\node at (axis cs:0,0,2.1) {$z$};
\end{axis}
\end{tikzpicture}
\end{document}
输出:
答案1
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = center,
ticks=none,
xmin=-2, xmax=2,
ymin=-2, ymax=2,
zmin=0, zmax=2,
view={135}{25},
xlabel={$x$},
ylabel={$y$},
zlabel={$z$},
xlabel style={at={(ticklabel* cs:1)}, anchor=east},
ylabel style={at={(ticklabel* cs:1)}, anchor=west},
zlabel style={at={(ticklabel* cs:1)}, anchor=south}
]
\end{axis}
\end{tikzpicture}
\end{document}
或这个:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = center,
ticks=none,
xmin=-2, xmax=2.2, % Adjusted xmax
ymin=-2, ymax=2.2, % Adjusted ymax
zmin=0, zmax=2,
view={135}{25}, % You might want to adjust this
]
% Adjusted label positions
\node at (axis cs:2.0,-0.25,0) {$x$}; % Moved x label a bit further
\node at (axis cs:0.25,2.0,0) {$y$}; % Moved y label a bit further
\node at (axis cs:0,0,2.1) {$z$};
\end{axis}
\end{tikzpicture}
\end{document}
或者您可以使用内置标签的 pgfplots:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = center,
ticks=none,
xmin=-2, xmax=2,
ymin=-2, ymax=2,
zmin=0, zmax=2,
view={135}{25},
xlabel={$x$},
ylabel={$y$},
zlabel={$z$},
]
\end{axis}
\end{tikzpicture}
\end{document}