[Tex/LaTex] How to plot data from a CSV file using tikz and csvsimple

csvcsvsimplepgfplotsplottikz-pgf

Here is a minimal example :

\begin{filecontents*}{data.csv}
a,b,c,d
1,4,5,1
2,3,1,5
3,5,6,1
4,1,4,9
5,3,4,7
\end{filecontents*}

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{csvsimple}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw plot coordinates {%
\csvreader[head to column names]{data.csv}{}{(\a,\b) }
};
\end{tikzpicture}
\end{document}

The command \csvreader[head to column names]{data.csv}{}{(\a,\b) } extracts coordinates (columns a and c) from the CSV file data.csv :

(1,4) (2,3) (3,5) (4,1) (5,3)

And yet, it seems that I can't plot the points above using tikz :

Package tikz Error: Cannot parse this coordinate

Any idea ?

Best Answer

If you need to plot data from files, I think you'll be much happier if you use PGFPlots instead of the native plot functionality of TikZ. Here's a very simple example of plotting your example data to get you started.

PGFPlots is very customizable, you can tweak virtually every aspect of your plots, and it's much more user-friendly than if you tried to knit everything yourself.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
a,b,c,d
1,4,5,1
2,3,1,5
3,5,6,1
4,1,4,9
5,3,4,7
\end{filecontents*}


\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot table [x=a, y=c, col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}