考虑下面的 MWE。蓝线在 上方延伸 -1
。我们可以将 y 刻度移到 RH 侧吗y<0
?怎么做?
\documentclass[a4paper]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-3.3,xmax=3.3,
ymin=-1.4,ymax=1.4,
axis x line=middle,
axis y line=middle,
ytick={-1,0,1},
]
\addplot[thick,blue,domain=\pgfkeysvalueof{/pgfplots/xmin}:0,samples=2] {-1};
\addplot[thick,red,domain=0:\pgfkeysvalueof{/pgfplots/xmax},samples=2] {1};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
您可以使用extra y tick
-1,然后更改ticklabel style
它的。
\documentclass[a4paper]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-3.3,xmax=3.3,
ymin=-1.4,ymax=1.4,
axis x line=middle,
axis y line=middle,
ytick={0,1},
extra y ticks={-1},
extra y tick style={tick label style={right=3pt}}
]
\addplot[thick,blue,domain=\pgfkeysvalueof{/pgfplots/xmin}:0,samples=2] {-1};
\addplot[thick,red,domain=0:\pgfkeysvalueof{/pgfplots/xmax},samples=2] {1};
\end{axis}
\end{tikzpicture}
\end{document}
另一种可能性
警告:接下来是丑陋的黑客行为。
\documentclass[a4paper,border=4mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-3.3,xmax=3.3,
ymin=-1.4,ymax=1.4,
axis x line=middle,
axis y line=middle,
ytick={-1,0,1},
yticklabel={%
\ifdim\tick pt <0pt
\rlap{\quad\axisdefaultticklabel}
\else
\axisdefaultticklabel
\fi
}
]
\addplot[thick,blue,domain=\pgfkeysvalueof{/pgfplots/xmin}:0,samples=2] {-1};
\addplot[thick,red,domain=0:\pgfkeysvalueof{/pgfplots/xmax},samples=2] {1};
\end{axis}
\end{tikzpicture}
\end{document}