我需要更改我的刻度标签的位置,因为它们不可读:
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,% axis y line=middle,
xtick = {-1-sqrt(6), -1+sqrt(6)},
xticklabels = {$-1-\sqrt{6}$, $-1+\sqrt{6}$},
ytick = {-5-2*sqrt(6)},
yticklabels = {$-5-2\sqrt{6}$},
xlabel = $x$, ylabel = $f(x)$, no markers, ]
\addplot+[thick, domain=-9:7,samples=80] {((x-1)*(x-2))/(x+1)};
\draw[<->,red] (axis cs:{-1-sqrt(6)},{-5-2*sqrt(6)}) +(axis direction cs:-1,0) --++(axis direction cs:1,0);
\end{axis}
\end{tikzpicture}
\end{document}
我希望 xticklabels 位于其轴的上方,而 yticklabels 位于其轴的右侧。
我尝试使用pos
键,但没有成功。然后我使用了right
ticklabelsabove
样式。它起作用了,但位置相对于原始刻度锚点进行了修改:结果,标签离刻度太近了:
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,% axis y line=middle,
xtick = {-1-sqrt(6), -1+sqrt(6)},
% xticklabel pos = top, % no effect
xticklabel style={above},
xticklabels = {$-1-\sqrt{6}$, $-1+\sqrt{6}$},
ytick = {-5-2*sqrt(6)},
yticklabels = {$-5-2\sqrt{6}$},
% yticklabel pos = right, % no effect
yticklabel style={right},
xlabel = $x$, ylabel = $f(x)$, no markers, ]
\addplot+[thick, domain=-9:7,samples=80] {((x-1)*(x-2))/(x+1)};
\draw[<->,red] (axis cs:{-1-sqrt(6)},{-5-2*sqrt(6)}) +(axis direction cs:-1,0) --++(axis direction cs:1,0);
\end{axis}
\end{tikzpicture}
\end{document}
实现此目的的正确方法是什么? 谢谢。
答案1
您只需添加xshift
和yshift
到样式中即可。
\documentclass{standalone}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.16} %<-maybe add
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,% axis y line=middle,
xtick = {-1-sqrt(6), -1+sqrt(6)},
xticklabel style={above,yshift=0.5ex},
xticklabels = {$-1-\sqrt{6}$, $-1+\sqrt{6}$},
ytick = {-5-2*sqrt(6)},
yticklabels = {$-5-2\sqrt{6}$},
yticklabel style={right,xshift=0.5ex},
xlabel = $x$, ylabel = $f(x)$, no markers, ]
\addplot+[thick, domain=-9:7,samples=80] {((x-1)*(x-2))/(x+1)};
\draw[<->,red] (axis cs:{-1-sqrt(6)},{-5-2*sqrt(6)}) +(axis direction cs:-1,0) --++(axis direction cs:1,0);
\end{axis}
\end{tikzpicture}
\end{document}
我也会使用compat=1.16
和unbounded coords=jump
。最重要的是xshift=\pgfkeysvalueof{/pgfplots/major tick length}
是使用由 Stefan Pinnow 建议。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16} %<- added
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,% axis y line=middle,
xtick = {-1-sqrt(6), -1+sqrt(6)},
xticklabel style={above,yshift=0.5ex},
xticklabels = {$-1-\sqrt{6}$, $-1+\sqrt{6}$},
ytick = {-5-2*sqrt(6)},
yticklabels = {$-5-2\sqrt{6}$},
yticklabel style={right,xshift=\pgfkeysvalueof{/pgfplots/major tick length}},
xlabel = $x$, ylabel = $f(x)$, no markers,
unbounded coords=jump]
\addplot+[thick, domain=-9:7,samples=81,smooth] {((x-1)*(x-2))/(x+1)};
\draw[<->,red] (axis cs:{-1-sqrt(6)},{-5-2*sqrt(6)}) +(axis direction cs:-1,0) --++(axis direction cs:1,0);
\end{axis}
\end{tikzpicture}
\end{document}