TikZ
我正在尝试通过节点在轴环境中进行一些注释PGFplots
。但是,节点的放置总是出错,请参见下面的最小工作示例。我该如何解决这个问题?如何TikZ
正确计算注释的坐标?
梅威瑟:
\documentclass[margin=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
xtick={-0.1,0,1,1.1},xmin=-0.1,xmax=1.1,xlabel=$x$,
ytick={-0.2,0,2,2.2},ymin=-0.2,ymax=2.2,ylabel style={rotate=-90},ylabel=$y$,
unit vector ratio=2 1 1,
]
\addplot[blue,domain=0:1] {x};
\addplot[red,domain=0:1] {2*x};
% Annotation.
\coordinate (SW) at (axis cs:-0.1,-0.2); % (S)outh (w)est.
\coordinate (SE) at (axis cs:1.1,-0.2); % (S)outh (e)ast.
\coordinate (NE) at (axis cs:1.1,2.2); % (N)orth (e)ast.
\coordinate (NW) at (axis cs:-0.1,2.2); % (N)orth (w)est.
\coordinate (A) at (axis cs:1,0);
\fill[black] (A) circle (1pt) node[anchor=south]{A};
% ??? WHY IS THIS NOT WORKING. SHOULD BE THE SAME AS NODE A. ???
\coordinate (B) at ($(SE)+(axis cs:-0.1,0.2)$); % ??? WHY IS THIS NOT WORKING. SHOULD BE THE SAME AS NODE A. ???
\fill[black] (B) circle (1pt) node[anchor=south east]{B}; % ??? WHY IS THIS NOT WORKING. SHOULD BE THE SAME AS NODE A. ???
% ??? WHY IS THIS NOT WORKING. SHOULD BE THE SAME AS NODE A. ???
\end{axis}
\end{tikzpicture}
\end{document}
答案1
画布区域的原点与绘图区域的原点不同。calc 表达式中的绘图坐标对 (-0.1, 0.2)($(SE) + (-0.1, 0.2)$)
被转换为画布坐标并添加到 的画布坐标中SE
。但由于原点不匹配,结果会因绘图原点到画布原点的位移而移动。实际上您想添加“相对”坐标:
($(SE) + (-0.1, 0.2) - (0, 0)$)
下面的例子试图说明这一点:
\documentclass[margin=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
xtick={-0.1,0,1,1.1},xmin=-0.1,xmax=1.1,xlabel=$x$,
ytick={-0.2,0,.5,.7},
ymin=-0.2,ymax=.7,
ylabel style={rotate=-90},
ylabel=$y$,
unit vector ratio=2 1 1,
]
% \addplot[blue,domain=0:1] {x};
% \addplot[red,domain=0:1] {2*x};
% Annotation.
\coordinate (SW) at (axis cs:-0.1,-0.2); % (S)outh (w)est.
\coordinate (SE) at (axis cs:1.1,-0.2); % (S)outh (e)ast.
\coordinate (NE) at (axis cs:1.1,2.2); % (N)orth (e)ast.
\coordinate (NW) at (axis cs:-0.1,2.2); % (N)orth (w)est.
\coordinate (A) at ($(1,0)$);
\fill[black] (A) circle (1pt) node[anchor=south]{A};
\coordinate (CO) at (0pt, 0pt);% canvas origin
\coordinate (PO) at (0, 0);% plot origin
\coordinate (D) at (-0.1, 0.2);% displacement as plot point
\begin{scope}[
-{Triangle[]},
node font=\footnotesize,
inner sep=.15em,
]
% Show canvas origin
\draw
(CO) ++(1em, .4em) node[above right] (tmp) {Canvas origin (CO)}
(tmp.west) -- (CO);
% Show plot origin
\draw
(PO) ++(1em, .4em) node[above right] (tmp) {Plot origin (PO)}
(tmp.west) -- (PO);
% Show displacement as absolute plot point
\draw
(D) ++(1em, .4em) node[above right] (tmp) {D}
(tmp.west) -- (D);
% Show SE
\draw
(SE) ++(-1.5em, .4em) node[above left] (tmp) {SE}
(tmp.east) -- (SE);
% Show SE + D
\draw
($(SE) + (D)$) coordinate (SE_D)
++(-1em, .4em) node[above left] (tmp) {SE + D}
(tmp.east) -- (SE_D);
% Show SE + D - plot origin
\draw
($(SE) + (D) - (PO)$) coordinate (SE_D_PO)
++(-1em, 0em) node[left] (tmp) {SE + D - PO}
(tmp.east) -- (SE_D_PO);
\draw[red, very thick, <->, >={latex[]}]
(CO) -- (D);
\draw[red, very thick, <->, >={latex[]}]
(SE) -- (SE_D);
\draw[blue, thick, <->, >={latex[]}]
(PO) -- (D);
\draw[blue, thick, <->, >={latex[]}]
(SE) -- (SE_D_PO);
\end{scope}
\end{axis}
\end{tikzpicture}
\end{document}
更清晰的符号是使用cs axis direction
如下坐标系回答的 esdd。
答案2
您必须使用axis direction cs
:
\coordinate (B) at ($(SE)+(axis direction cs:-0.1,0.2)$);
代码:
\documentclass[margin=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
xtick={-0.1,0,1,1.1},xmin=-0.1,xmax=1.1,xlabel=$x$,
ytick={-0.2,0,2,2.2},ymin=-0.2,ymax=2.2,ylabel style={rotate=-90},ylabel=$y$,
unit vector ratio=2 1 1,
]
\addplot[blue,domain=0:1] {x};
\addplot[red,domain=0:1] {2*x};
% Annotation.
\coordinate (SW) at (-0.1,-0.2); % (S)outh (w)est.
\coordinate (SE) at (1.1,-0.2); % (S)outh (e)ast.
\coordinate (NE) at (1.1,2.2); % (N)orth (e)ast.
\coordinate (NW) at (-0.1,2.2); % (N)orth (w)est.
\coordinate (A) at (1,0);
\fill[black] (A) circle (1pt) node[anchor=south]{A};
% ??? WHY IS THIS NOT WORKING. SHOULD BE THE SAME AS NODE A. ???
\coordinate (B) at ($(SE)+(axis direction cs:-0.1,0.2)$);??? WHY IS THIS NOT WORKING. SHOULD BE THE SAME AS NODE A. ???
\fill[red,fill opacity=.5] (B) circle (2pt) node[anchor=south east]{B}; % ??? WHY IS THIS NOT WORKING. SHOULD BE THE SAME AS NODE A. ???
% ??? WHY IS THIS NOT WORKING. SHOULD BE THE SAME AS NODE A. ???
\end{axis}
\end{tikzpicture}
\end{document}