How to plot data from a CSV file using tikz and csvsimple?
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}
It seems that it's impossible to call csvreader
inside \draw plot coordinates { }
. To avoid the plot
command and still being able to plot lines between points, I need to use xdef
to remember the previous point.
\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}
\csvreader[ head to column names,%
late after head=\xdef\aold{\a}\xdef\bold{\b},%
after line=\xdef\aold{\a}\xdef\bold{\b}]%
{data.csv}{}{%
\draw (\aold, \bold) -- (\a,\b) node {$\times$};
}
\end{tikzpicture}
\end{document}
I get the output below. Now I can get back to work and print a whole bunch of data in my report ;-) I love it !
Another example with csvsimple, tikz and pgf-pie.
\begin{filecontents*}{newData.csv}
20,30,50
\end{filecontents*}
\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{csvsimple}
\usepackage{tikz}
\usepackage{pgf-pie}
\begin{document}
\begin{tikzpicture}
\csvreader[no head]%
{newData.csv}
{1=\colVali,2=\colValii,3=\colValiii}
{%
\pie[polar, explode=0.1]
{\colVali/A, \colValii/B, \colValiii/C}
}
\end{tikzpicture}
\end{document}
Which produces the following pie chart: