How to use a string column with pgfplotstable to label datapoints in a pgfplot?

5,769

You need to add value prefix if you don't want the contents to be parsed as numbers.

\documentclass{article}
\usepackage{pgfplots,pgfplotstable,filecontents}
\begin{filecontents*}{table.csv}
Name,X,Y,Z
ODIN,250,45,2001
NIDO,280,70,2002
DINO,270,60,2003
\end{filecontents*}
\begin{document}

\pgfkeys{/pgf/number format/set thousands separator={}}
\pgfplotstabletypeset[columns/Name/.style={string type},col sep=comma]{table.csv}


\begin{tikzpicture}
    \begin{axis}[
    visualization depends on={value \thisrow{Name} \as \labela}, % note the value prefix
    visualization depends on={\thisrow{Z} \as \labelb},
    every node near coord/.append style={font={\tiny}},
    nodes near coords={\labela/\pgfmathprintnumber\labelb},
    nodes near coords align={horizontal}
        ]

    \addplot[scatter,only marks] table [x=X,y=Y,col sep=comma] {table.csv};

    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Share:
5,769

Related videos on Youtube

Ingo
Author by

Ingo

Updated on June 19, 2020

Comments

  • Ingo
    Ingo almost 3 years

    I have a comma-separated list of data points. They should be plotted as a scatter plot, and should be labeled using data from the .csv file. Having specified the first column of my file as string type, I still keep running into errors saying Could not parse input [...] as a floating point number, sorry.

    Given a CSV file of the following format, how can I read Name and Z as labels?

    Name,X,Y,Z
    ODIN,250,45,2001
    

    Here is my MWE:

    \documentclass{article}
    
    \usepackage{tikz,pgfplots,pgfplotstable}
        \pgfplotsset{compat=1.5.1}
    
    \begin{document}
    
    \pgfplotstabletypeset[columns/Name/.style={string type}]{table.csv};
    
    \begin{tikzpicture}
        \begin{axis}[
        visualization depends on=\thisrow{Name} \as \labela,
        visualization depends on=\thisrow{Z} \as \labelb,
        every node near coord/.append style={font={\tiny}},
        nodes near coords=\labela/\labelb,
        nodes near coords align={horizontal},]
    
        \addplot[only marks] table [x=X,y=Y,col sep=comma] {table.csv};
    
        \end{axis}
    \end{tikzpicture}
    
    \end{document}
    

    Note that if the data in column Name is just floats it should work fine.

  • Ingo
    Ingo almost 11 years
    That's great to know, thanks. Can you tell me how you found that out?
  • percusse
    percusse almost 11 years
    @Ingo I've struggled with this before. It's also in the manual (at the top of page 284 ver. 1.5.1) Much easier when you know what to look for :)