Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to plot some graphs in pgfplots and want to place nodes on the exact same x coordinate of the plot. The position of the note has to be set in x direction but free in y direction. Here is one example of the way it shouldn't be.

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{figure} 
\begin{tikzpicture}    
\begin{axis}
    \addplot {-x}  [yshift=8pt] node[pos=0.1] {$0,1$};
    \addplot {x^2} [yshift=8pt] node[pos=0.1] {$0,1$};
    \addplot{3*x^2}[yshift=8pt] node[pos=0.1] {$0,1$};
\end{axis} 
\end{tikzpicture} 
\end{figure}

\end{document}

enter image description here


With the example linked by Jake I adapted the file. Now it is producing the return value 1 and no good result. What is wrong?

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{figure} 
\begin{tikzpicture}    
\begin{axis}
\addplot[yshift=8pt,add node at x={-4}{$0,1$}] {-x};
\addplot[yshift=8pt,add node at x={-4}{$0,1$}] {x^2};
\addplot[yshift=8pt,add node at x={-4}{$0,1$}] {3*x^2};
\end{axis} 
\end{tikzpicture} 
\end{figure}

\end{document}
share|improve this question
1  
You can use the (axis cs:x,y) to use the axis coordinate system to place your nodes. –  percusse Jan 14 '13 at 22:42
    
Welcome to TeX.SE. To add to @percusse's comment: you need to place them separately form the \addplot via something like \node at (axis cs: 0,20) {$f(x)$}; which places the node at (0,20), for example. –  Peter Grill Jan 14 '13 at 22:43
1  
Related/duplicate? pgfplots: Placing node on a specific x-position –  Jake Jan 14 '13 at 23:33
    
With (axis cs:x,y) I place the note on a fix coordinate position. But I want to note the graph on a defined x position and the y position of the note is set by the function. I'm looking for something like node[xpos=-4]. –  Nils Jan 15 '13 at 11:26
1  
@Nils: That's what the solution does that I linked to in my previous comment. –  Jake Jan 15 '13 at 12:02
add comment

1 Answer

You can use the code from the answer to pgfplots: Placing node on a specific x-position:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{intersections}

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

\begin{document}

\begin{figure} 
\begin{tikzpicture}    
\begin{axis}
\addplot[add node at x={-4}{[fill=white]{$0,1$}}] {-x};
\addplot[add node at x={-4}{[fill=white]{$0,1$}}] {x^2};
\addplot[add node at x={-4}{[fill=white]{$0,1$}}] {3*x^2};
\end{axis} 
\end{tikzpicture} 
\end{figure}

\end{document}
share|improve this answer
    
That's the answer, thank you very much! :) –  Nils Jan 15 '13 at 21:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.