我想在堆叠多个轴时节省空间。这些图具有共同的域,我不想因为重复相同的信息(x 轴上的数字)而失去空间。是否可以禁用刻度的 x 编号?到目前为止,我发现了这个选项,\empty
但是这会删除刻度,因此无法绘制它们的网格。我想保留网格。
\begin{figure}[!h]
\centering
\pgfplotsset{width={\textwidth},height={6 cm},
xmin=0,xmax=7}
\begin{tikzpicture}
\begin{axis}[
ylabel={$y$},
grid=major,
%xtick=\empty,
]
\addplot[blue, domain=2:4] {1};
\addplot[red, dashed, domain=2-2:4-2] {2*x};
\end{axis}
\end{tikzpicture}\vspace{.5 cm}
\begin{tikzpicture}
\begin{axis}[
ylabel={$y$},
grid=major,
]
\addplot[blue, domain=2:4] {1};
\addplot[red, dashed, domain=2-1:4-1] {2*x};
\end{axis}
\end{tikzpicture}\vspace{.5 cm}
\begin{tikzpicture}
\begin{axis}[
xlabel= {$x$}, ylabel={$y$},
grid=major,
]
\addplot[blue, domain=2:4] {1};
\addplot[red, dashed, domain=2:4] {2*x};
\end{axis}
\end{tikzpicture}
\label{fig:sin}
\end{figure}
答案1
您可以使用xticklabels={}
:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\pgfplotsset{width={\textwidth},height={6 cm},
xmin=0,xmax=7}
\begin{tikzpicture}
\begin{axis}[
ylabel={$y$},
grid=major,
xticklabels={},
]
\addplot[blue, domain=2:4] {1};
\addplot[red, dashed, domain=2-2:4-2] {2*x};
\end{axis}
\end{tikzpicture}
\end{document}