1

I am unable to take data from a .csv file and generate a simple scatter plot. The file contents are:

Group   x   y
Group1  10  1.1
Group2  20  8.1
Group3  9   0
Group1  11  2
Group2  20  8.2
Group3  8   0.2
Group1  11  2
Group2  20  9.1
Group3  9   0.1

The code is:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{statistics}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    grid=major,
    grid style={dotted},
    table/col sep=comma,
    scatter/classes={%
    Group1={brown, mark=*, mark size=1.5pt},
    Group2={black, mark=x, mark size=2.5pt},
    Group3={red, mark=square*, mark size=1pt}},
    scatter, only marks,
    scatter src=explicit symbolic
]
\addplot+ [] table[header=true, only marks, x=x, y=y,  meta=Group] {Test.csv};%
\end{axis}
\end{tikzpicture}
\end{document}

I keep getting the error:

Sorry, could not retrieve column 'x' from table '\\pgfplotstableread@filename@@table@name '. Please check spelling (or introduce name aliases)..
1
  • 1
    csv = comma separated values
    – JamesT
    Feb 19 at 15:03

1 Answer 1

1

enter image description here

\begin{filecontents}[overwrite]{Test.csv}
Group,x,y
Group1,10,1.1
Group2,20,8.1
Group3,9,0
Group1,11,2
Group2,20,8.2
Group3,8,0.2
Group1,11,2
Group2,20,9.1
Group3,9,0.1
\end{filecontents}

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{statistics}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    grid=major,
    grid style={dotted},
    table/col sep=comma,
    scatter/classes={%
    Group1={brown, mark=*, mark size=1.5pt},
    Group2={black, mark=x, mark size=2.5pt},
    Group3={red, mark=square*, mark size=1pt}},
    scatter, only marks,
    scatter src=explicit symbolic
]
\addplot+ [] table[header=true, only marks, x=x, y=y,  meta=Group] {Test.csv};%
\end{axis}
\end{tikzpicture}
\end{document}

If you want to keep the original .csv structure of spaces then change to this in options:

table/col sep=space,

enter image description here

\begin{filecontents}{test.csv}
Group   x   y
Group1  10  1.1
Group2  20  8.1
Group3  9   0
Group1  11  2
Group2  20  8.2
Group3  8   0.2
Group1  11  2
Group2  20  9.1
Group3  9   0.1
\end{filecontents}

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{statistics}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    grid=major,
    grid style={dotted},
    table/col sep=space,
    scatter/classes={%
    Group1={brown, mark=*, mark size=1.5pt},
    Group2={black, mark=x, mark size=2.5pt},
    Group3={red, mark=square*, mark size=1pt}},
    scatter, only marks,
    scatter src=explicit symbolic
]
\addplot+ [] table[header=true, only marks, x=x, y=y,  meta=Group] {test.csv};%
\end{axis}
\end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .