[Tex/LaTex] Plot and label data from file with PGFPlots

pgfplotstikz-pgf

Given some data from a file, I would like to use the first two columns as coordinates, and the second two columns as label for every point. The data is given in this form:

x                        y                        labela                   labelb
1.416197036356566059e+02 5.144315570548267715e+03 7.000000000000000000e+00 1.000000000000000000e+00

I use the table command to read the first two columns as data:

\begin{tikzpicture}
    \begin{axis}
        \addplot+[only marks,x=x, y=y, ] table {DataTable.dat};
    \end{axis}
\end{tikzpicture}

The data of the second column should appear above each scatter point as simple text or may be in a bubble like this: labela / labelb.

A promising option seems to be the option nodes near coords which I use to show the y-coordinate of points in other plots, but I don't see how I can use the data from the file and combine it with that.

Best Answer

nodes near coords is the right approach. You can make your two label columns available inside the label nodes using the key visualization depends on=\thisrow{<column name>} \as \<macro name>. The key can be called as often as you like.

Then, you can typeset the labels using nodes near coords=\pgfmathprintnumber{\<macronameA>} / \pgfmathprintnumber{\<macronameB>}. The styles for the label nodes can be set using every node near coord/.append style=<styles>.

Note that the label nodes will not automatically increase the axis limits, so they might overlap the axes. In that case, you'll need to use enlargelimits or enlarge y limits.

\documentclass{article}

\usepackage{pgfplots}

\begin{filecontents}{DataTable.dat}
x                        y                        labela                   labelb
1.416197036356566059e+02 5.144315570548267715e+03 7.000000000000000000e+00 1.000000000000000000e+00
2.416197036356566059e+02 3.144315570548267715e+03 8.000000000000000000e+00 4.000000000000000000e+00
4.416197036356566059e+02 1.144315570548267715e+03 6.000000000000000000e+00 2.000000000000000000e+00
\end{filecontents}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[enlarge y limits={upper,value=0.3}]
        \addplot+[
            only marks,
            visualization depends on=\thisrow{labela} \as \labela,
            visualization depends on=\thisrow{labelb} \as \labelb,
            nodes near coords=\pgfmathprintnumber{\labela}/\pgfmathprintnumber{\labelb},
            every node near coord/.append style={
                black,
                draw,
                circle,
                inner sep=1pt,
                yshift=1ex
            }
            ] table {DataTable.dat};
    \end{axis}
\end{tikzpicture}

\end{document}