我正在使用 pgfplots 绘制简单函数。我的轴设置如下:
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-30,xmax=30,
ymin=-30,ymax=30,
grid=both,
axis lines=middle,
xtick distance=4,
ytick distance=4,
enlargelimits={abs=0.5},
axis line style={latex-latex},
ticklabel style={font=\scriptsize}
]
\end{axis}
\end{tikzpicture}
\end{document}
由此产生了如下结果:
我的问题是,许多这些刻度标签都是多余的。我怎样才能仅显示和处的刻度x = 4
标签y = 4
?
要明确的是,我想保留刻度线本身和网格线,我只想去掉数字8
、12
等、和-4
、、等-8
。-12
答案1
另一种方法是将以下选项添加到您的axis
环境中:
xticklabels={},
yticklabels={},
extra x ticks={4},
extra y ticks={4},
前两行隐藏所有刻度标签,后两行添加额外的刻度。以下代码
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-30,xmax=30,
ymin=-30,ymax=30,
grid=both,
axis lines=middle,
xtick distance=4,
ytick distance=4,
enlargelimits={abs=0.5},
axis line style={latex-latex},
ticklabel style={font=\scriptsize},
xticklabels={},
extra x ticks={4},
yticklabels={},
extra y ticks={4},
]
\end{axis}
\end{tikzpicture}
\end{document}
给出
答案2
干得好:
\documentclass{standalone}
\usepackage{pgfplots,tikz}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-30,xmax=30,
ymin=-30,ymax=30,
grid=both,
axis lines=middle,
xtick distance=4,
ytick distance=4,
xtick={-28,-24,-20,-16,-12,-8,-4,0,4,8,12,16,20,24,28},
xticklabels={,,,,,,,,4,,,,,,},
ytick={-28,-24,-20,-16,-12,-8,-4,0,4,8,12,16,20,24,28},
yticklabels={,,,,,,,,4,,,,,,},
enlargelimits={abs=0.5},
axis line style={latex-latex},
ticklabel style={font=\scriptsize}
]
{}\end{axis};
\end{tikzpicture}
\end{document}