2

I would like to create a scatter plot with pgfplots, in which a text node is placed for every marker. The nodes should be at the same y-value of the respective marker, but at a fixed x-coordinate. The nodes' content should be the corresponding value from the 'label' column of the data table.

The MWE shows how far I could get:

\documentclass{article} 
\usepackage{pgfplots}

\begin{filecontents*}{exp1.txt}
x   y   label
0.33246562684995834 -1  0.33247(5)
0.33284098219518293 -2  0.33284(5)
0.3319866331064203  -3  0.33199(5)
0.33301936261400417 -4  0.33302(5)
0.3343605877669449  -5  0.33436(5)
0.33419390169962254 -6  0.33419(5)
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmin=0.331, xmax=0.339, ytick=data,
    visualization depends on={value \thisrow{label} \as \labela},
    nodes near coords={\labela},
    nodes near coords align={horizontal}]
    \addplot+[scatter, only marks] table[x=x,y=y] {exp1.txt};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

So the question is: How can I 'detach' the x-coordinates of the text nodes from the markers and align all of them to the right of a given x-value?

3

There might be more elegant ways of doing this, but this works at least. See some comments in the code.

enter image description here

\documentclass{article} 
\usepackage{pgfplots}

\begin{filecontents*}{exp1.txt}
x   y   label
0.33246562684995834 -1  0.33247(5)
0.33284098219518293 -2  0.33284(5)
0.3319866331064203  -3  0.33199(5)
0.33301936261400417 -4  0.33302(5)
0.3343605877669449  -5  0.33436(5)
0.33419390169962254 -6  0.33419(5)
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmin=0.331, xmax=0.339, ytick=data,
    visualization depends on={value \thisrow{label} \as \labela},,
    nodes near coords align={horizontal},
    xticklabel style={/pgf/number format/precision=3} % the xticklabels were duplicated
]
    % plot only the dots
    \addplot+[only marks] table[x=x,y=y] {exp1.txt};
    % use x expr to set a fixed x-value, and set mark=none to only get the node near coord
    \addplot [nodes near coords={\labela}, only marks,mark=none] table[x expr=0.338,y=y] {exp1.txt};
\end{axis}
\end{tikzpicture}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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