在下面的 MWE 中,我希望节点垂直对齐。给它们相同的值pos
似乎无法做到这一点。我可以尝试通过pos
使用反复试验来调整它们,但这很乏味,特别是因为我要构建几个类似的图。处理这个问题的最佳方法是什么?
(我发现这种情况会发生,\addplot table
但不适用于\addplot
公式。但我还没有进行过广泛的测试。)
\documentclass[border=5]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{filecontents}{mwe.dat}
-6.9282,486.63,682.22
-4.0426,373.74,569.33
-1.1569,260.82,456.4
1.7287,147.95,343.53
4.6144,46.862,230.63
7.5,41.138,118.85
10.386,52.68,43.39
13.271,64.223,44.245
16.157,75.765,55.764
19.043,87.308,67.306
21.928,98.851,78.848
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[black,mark=none] table [col sep=comma,x index=0,y index=1] {mwe.dat}
node[above,pos=0.95] {$f_1(x)$};
\addplot[mark=none,black] table [col sep=comma,x index=0,y index=2] {mwe.dat}
node[below,pos=0.95] {$f_2(x)$};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
您喜欢以下职位吗:
这是通过(仅摘录了 MWE)实现的
\begin{axis}
\addplot[black,mark=none] table [col sep=comma,x index=0,y index=1] {mwe.dat}
node[above left] {$f_1(x)$};
\addplot[mark=none,black] table [col sep=comma,x index=0,y index=2] {mwe.dat}
node[below left] {$f_2(x)$};
\end{axis}
编辑:由于您使用 tikzlibraty 定位,您也可以尝试宽度:
\begin{axis}
\addplot[black,mark=none] table [col sep=comma,x index=0,y index=1] {mwe.dat}
node[above left=-1mm and 3mm] {$f_1(x)$};
\addplot[mark=none,black] table [col sep=comma,x index=0,y index=2] {mwe.dat}
node[below left= 1mm and 3mm] {$f_2(x)$};
\end{axis}
将机器人节点向左移动相同的距离。
无论图形中的曲线是如何确定的(通过函数、坐标或表格),它们(在您的情况下)的端点都位于同一x
坐标。使用参数left, above, below, ...
,您可以说它必须位于端点的左侧、上方或下方。
根据您所需的参数pos=...
,节点所锚定的点(在您的情况下)是曲线长度的 0.95。显然您的曲线长度不一样,因此它们不是水平对齐的。
借助positioning
库,您可以相对于曲线末端移动节点,例如将above left=-1mm and 3mm
其移动到曲线末端下方 1 毫米处并向左移动 3 毫米。
答案2
您还可以使用 Jake 定义的键这里。
\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{shapes.geometric, intersections}
\pgfplotsset{compat=1.7}
\begin{filecontents}{mwe.dat}
-6.9282,486.63,682.22
-4.0426,373.74,569.33
-1.1569,260.82,456.4
1.7287,147.95,343.53
4.6144,46.862,230.63
7.5,41.138,118.85
10.386,52.68,43.39
13.271,64.223,44.245
16.157,75.765,55.764
19.043,87.308,67.306
21.928,98.851,78.848
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\makeatletter
\def\parsenode[#1]#2\pgf@nil{%
\tikzset{label node/.style={#1}}
\def\nodetext{#2}
}
\tikzset{
add node at x/.style 2 args={
name path global=plot line,
/pgfplots/execute at end plot visualization/.append={
\begingroup
\@ifnextchar[{\parsenode}{\parsenode[]}#2\pgf@nil
\path [name path global = position line #1-1]
({axis cs:#1,0}|-{rel axis cs:0,0}) --
({axis cs:#1,0}|-{rel axis cs:0,1});
\path [xshift=1pt, name path global = position line #1-2]
({axis cs:#1,0}|-{rel axis cs:0,0}) --
({axis cs:#1,0}|-{rel axis cs:0,1});
\path [
name intersections={
of={plot line and position line #1-1},
name=left intersection
},
name intersections={
of={plot line and position line #1-2},
name=right intersection
},
label node/.append style={pos=1}
] (left intersection-1) -- (right intersection-1)
node [label node]{\nodetext};
\endgroup
}
},
add node at y/.style 2 args={
name path global=plot line,
/pgfplots/execute at end plot visualization/.append={
\begingroup
\@ifnextchar[{\parsenode}{\parsenode[]}#2\pgf@nil
\path [name path global = position line #1-1]
({axis cs:0,#1}-|{rel axis cs:0,0}) --
({axis cs:0,#1}-|{rel axis cs:1,1});
\path [yshift=1pt, name path global = position line #1-2]
({axis cs:0,#1}-|{rel axis cs:0,0}) --
({axis cs:0,#1}-|{rel axis cs:1,1});
\path [
name intersections={
of={plot line and position line #1-1},
name=left intersection
},
name intersections={
of={plot line and position line #1-2},
name=right intersection
},
label node/.append style={pos=1}
] (left intersection-1) -- (right intersection-1)
node [label node] {\nodetext};
\endgroup
}
}
}
\makeatother
\begin{axis}
\addplot [
add node at x={20}{[above]{$f_1(x)$}},
add node at y={300}{[left]{$f_1(x)$}}
]table[col sep=comma,x index=0,y index=1] {mwe.dat};
\addplot[
add node at x={20}{[below]{$f_2(x)$}},
add node at y={300}{[right]{$f_2(x)$}}] table [col sep=comma,x index=0,y index=2] {mwe.dat};
\end{axis}
\end{tikzpicture}
\end{document}