4

In the MWE below, I would like the nodes to align vertically. Giving them the same pos doesn't seem to do that. I can try to align them by adjusting the pos using trial and error but this is tedious, especially since I have several similar plots to build. What's the best way to handle this?

(I have found this to happen with \addplot table but not using \addplot with a formula. But I haven't tested that extensively.)

\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}  

enter image description here

jak123
  • 4,252
  • 5
  • 26
  • 49
  • You are right that pos is acting weird. pos=0 and pos=1 mark the ends, but every value in between is not proportional by any criteria I can think of. – John Kormylo Jan 18 '16 at 04:45

2 Answers2

2

Do you liked the following position:

enter image description here

It is achieved by (only snipped of 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}

Edit: Since you use tikzlibraty `positioning, you also can try width:

    \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}

which moves bot nodes for equal distance to the left.

enter image description here

Regardless how your curves in graph are determined (by function, coordinates or table) they have (in your case) their ends at same x-coordinate. With parameter left, above, below, ... you say, that it has to be left, above, below of their ends.

With argument pos=... you require, that point, to which node is anchored is (in your case) 0.95 length of curve. Apparently your curves haven't the same length, so they are not horizontally aligned.

With help of positioning library, you can move nodes in respect to the end of curves for example by above left=-1mm and 3mm it is mojed 1mm below of curve end and 3mm to left direction.

Zarko
  • 296,517
0

You can also use the keys defined by Jake here.

\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}

at xy

Ktree
  • 688