使用中间轴的相反刻度标签位置

使用中间轴的相反刻度标签位置

我需要更改我的刻度标签的位置,因为它们不可读:

\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键,但没有成功。然后我使用了rightticklabelsabove样式。它起作用了,但位置相对于原始刻度锚点进行了修改:结果,标签离刻度太近了:

\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

您只需添加xshiftyshift到样式中即可。

\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.16unbounded 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}

在此处输入图片描述

相关内容