我以为我理解了++
tikz/pgf 中的语法是如何工作的,但显然我不明白。请考虑以下示例代码,根据我的理解,它应该产生两条垂直的蓝线。
\documentclass{standalone}
\usepackage{pgfplots,tikz}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymin=0,ymax=1,
domain=1:4,
]
\addplot[red] {0.5};
\draw[blue] (axis cs:2.5,0.0) -- ++(axis cs:0,0.5);
\draw[blue] (axis cs:1.5,0.0) -- ++(axis cs:0,0.5);
\end{axis}
\end{tikzpicture}
\end{document}
然而,结果却是这样的:
蓝线倾斜了。这是为什么?
答案1
您必须使用axis direction cs
相对坐标:
\documentclass{standalone}
\usepackage{pgfplots}% loads also tikz
\pgfplotsset{compat=1.15}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymin=0,ymax=1,
domain=1:4,
]
\addplot[red] {0.5};
\draw[blue] (2.5,0.0) -- ++(axis direction cs:0,0.5);
\draw[blue] (1.5,0.0) -- ++(axis direction cs:0,0.5);
\end{axis}
\end{tikzpicture}
\end{document}
结果: