67

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 ?

4
  • Why simple way when there is complex methods? May be this answer -- tex.stackexchange.com/a/83740/11232 is useful (that uses pgfplotstable)
    – user11232
    Nov 23, 2012 at 2:17
  • I don't see any data printed with csvsimple package. Can you make it work if you use it outside the TikZ picture?
    – percusse
    Nov 23, 2012 at 3:16
  • \begin{tikzpicture} \csvreader[head to column names]{data.csv}{}{% \draw (\a,\b) node {$\times$}; } \end{tikzpicture} will work but I need to draw segments, not points. So I need a way to remember the previous point like in the csvsimple examples (not hard, but I don't know LaTeX that well).
    – remjg
    Nov 23, 2012 at 11:11
  • @HarishKumar I had a look at it, but I had so much trouble getting use to csvsimple... So I don't want to use an other package now ;-)
    – remjg
    Nov 23, 2012 at 11:32

5 Answers 5

84

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}
6
  • @RémiG.: Good decision =). If you have questions, don't hesitate to post them on the site, there are plenty of people with lots of PGFPlots expertise around here.
    – Jake
    Nov 23, 2012 at 12:19
  • I still face an issue. The same potion of code doesn't work inside a macro : File ended while scanning use of \pgfplots@addplotimpl@table@fromfile. Maybe I should ask another question...
    – remjg
    Nov 23, 2012 at 12:39
  • @RémiG.: If I wrap everything between \begin{tikzpicture} and \end{tikzpicture} in a \newcommand, it works fine. Maybe posting a new question is indeed a good idea.
    – Jake
    Nov 23, 2012 at 13:01
  • 2
    So I needed to add \shorthandoff{;} inside the tikzpicture environment to avoid problem with [frenchb]babel. The solution is explained here : tex.stackexchange.com/questions/74860/…. I'm all good now, thank you for your help !
    – remjg
    Nov 23, 2012 at 14:20
  • 7
    Shouldn't y=c be y=b here? Otherwise I can't see the relation between the code and the shown plot.
    – gosr
    May 1, 2015 at 16:26
9

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 !

Plot of CSV data using <code>tikz</code> and <code>csvsimple</code>

4

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:

enter image description here

3

Another example, based on a request for drafting a practical load curve of electricity, example that requires some style manipulations that are not very concentrated in the examples of the books, so it is a compilation of the manuals taken from here and there.

RESULT: enter image description here

MWE:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
\pgfplotsset{compat=1.14}

\begin{document}
    \begin{filecontents}{data01.dat}
        0   5          
        1    8       
        2    6        
        3    7        
        4    6        
        5    7      
        6    10        
        7    11         
        8    14       
        9    15        
        10   12       
        11   11       
        12   4       
        13   5        
        14   10      
        15   11         
        16   13           
        17   14        
        18   13       
        19   20      
        20   24           
        21   18       
        22   19        
        23   15     
        24   5          
    \end{filecontents}

    \begin{tikzpicture}[
        %Environment Cfg.
        font=\bfseries\sffamily,
    ]
    \begin{axis}[
        width=12cm,
        height=8cm,
        at={(0,0)},
        ymin=0,
        ymax=30,
        xmin=0,
        xmax=30,
        grid=both,
        minor tick num =5,
        minor tick style={draw=none},
        minor grid style={thin,color=black!10},
        major grid style={thin,color=black!10},
        ylabel={L\\O\\A\\D\\[5pt] kW.},
        xlabel=Time in Hours,
        tick align=outside,
        axis x line*=middle,
        axis y line*=none,
        xtick={0,5,...,30},
        ytick={0,5,...,30},
        xlabel style={color=blue!50!cyan},
        ylabel style={align=center,rotate=-90,color=blue!50!cyan},
        x tick label style={
            /pgf/number format/assume math mode, font=\sf\scriptsize},
        y tick label style={
        /pgf/number format/assume math mode, font=\sf\scriptsize},
    ]
    \addplot[color=blue!50!cyan,smooth,tension=0.7,very thick] table [x index=0,y index=1,col sep=space] {data01.dat};
    \addplot[color=cyan!50!lime,very thick] coordinates{(0,5)(25,5)};
    \addplot[color=orange,very thick] coordinates{(0,11)(25,11)};
    \addplot[color=red!80!orange,very thick] coordinates{(19,24.2)(23,24.2)};
    \node[text=cyan!50!lime,fill=white,align=center,anchor=west,scale=0.8,inner sep=5pt] at (24.5,5){Base\\ Load};
    \node[color=orange,fill=white,align=center,anchor=west,scale=0.8,inner sep=5pt] at (24.5,11){Average\\ Load};
    \node[color=red!80!orange,fill=white,align=center,anchor=west,scale=0.8,inner sep=5pt] at (21.2,24.2){Maxium\\ Load};
    \end{axis}

    \end{tikzpicture}
\end{document} 
0
-1

If you want to plot figure using external csv files. You have to put \usepackage{pgfplots} on the head of the tex file.

There is a demo

data.csv

x,y
1,0.1
2,0.2
3,0.3
4,0.4
5,0.5
6,0.6
7,0.7
8,0.8
9,0.9
10,1.0

Demo.tex

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

\usepackage{tikz}
\usetikzlibrary{arrows.meta,decorations.pathmorphing,backgrounds,positioning,fit,petri}

\usepackage{pgfplots} % it is a must when it needs reading csv files

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

enter image description here

You must log in to answer this question.

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