如何删除 x 轴上的标签 -1,4?如何删除 y 轴上的标签 -4,4?
\documentclass[tikz]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\usetikzlibrary[arrows.meta,bending]
\usetikzlibrary{shapes.geometric,positioning,shapes}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
restrict x to domain=-1:4, xmax=4, xmin=-1,
restrict y to domain=-4:4, ymax=4, ymin=-4,
x=2cm,
y=2cm,
axis x line = middle,
axis y line = middle,
major tick style=black,
axis line style =ultra thick,
major grid style=darkgray,
minor y tick num={3},
minor x tick num={3},
grid=both,
y tick label style= {/pgf/number format/1000 sep={}},
xtick={-1,0,...,4},
ytick={-4,-3,...,4},
samples=5000,
>=stealth,
]
\node[left] at (axis cs:0,3.8) {$y$};
\node[below] at (axis cs:3.8,0){$x$};
\node[below left] at (axis cs:-0.1,-0.05) {$0$};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
一种可能性是
xtick={-1,0,...,4},
xticklabel={
\ifdim\tick pt=-1pt
\else \ifdim\tick pt=4pt
\else \axisdefaultticklabel
\fi\fi},
ytick={-4,-3,...,4},
yticklabel={
\ifdim\tick pt=-4pt
\else \ifdim\tick pt=4pt
\else \axisdefaultticklabel
\fi\fi},
代码:
\documentclass[tikz]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\usetikzlibrary[arrows.meta,bending]
\usetikzlibrary{shapes.geometric,positioning,shapes}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
restrict x to domain=-1:4, xmax=4, xmin=-1,
restrict y to domain=-4:4, ymax=4, ymin=-4,
x=2cm,
y=2cm,
axis x line = middle,
axis y line = middle,
major tick style=black,
axis line style =ultra thick,
major grid style=darkgray,
minor y tick num={3},
minor x tick num={3},
grid=both,
y tick label style= {/pgf/number format/1000 sep={}},
xtick={-1,0,...,4},
xticklabel={
\ifdim\tick pt=-1pt
\else \ifdim\tick pt=4pt
\else \axisdefaultticklabel
\fi\fi},
ytick={-4,-3,...,4},
yticklabel={
\ifdim\tick pt=-4pt
\else \ifdim\tick pt=4pt
\else \axisdefaultticklabel
\fi\fi},
samples=5000,
>=stealth,
]
\node[left] at (axis cs:0,3.8) {$y$};
\node[below] at (axis cs:3.8,0){$x$};
\node[below left] at (axis cs:-0.1,-0.05) {$0$};
\end{axis}
\end{tikzpicture}
\end{document}
您还可以使用
xticklabel={
\ifdim\tick pt<-0.5pt
\else \ifdim\tick pt>3.5pt
\else \axisdefaultticklabel
\fi\fi}
另一种可能性是删除所有刻度标签并使用额外的刻度来设置所需的刻度标签:
xtick={-1,0,...,4},
xticklabel=\empty,
extra x ticks={1,...,3},
extra x tick label={\axisdefaultticklabel},
extra x tick style={grid=none,tickwidth=0pt},
ytick={-4,-3,...,4},
yticklabel=\empty,
extra y ticks={-3,...,3},
extra y tick label={\ifdim\tick pt=0pt\else\axisdefaultticklabel\fi},
extra y tick style={grid=none,tickwidth=0pt},
结果和上面一样。