# Area chart in LaTeX but with an unknown number of columns

I wanted to draw an area chart like the one seen in this question, but I want to use a file like this one, where the number of columns is unknown:

Generation,Dog,Cat
0,100,4800
1,200,4700
2,100,4400
3,100,3800
4,100,2800


The file could also look like this:

Generation,Dog,Cat,Elefant
0,100,4800,100
1,200,4700,100
2,100,4400,400
3,100,3800,1100
4,100,2800,2100


How would I go about doing that?

I am also kind of new to LaTeX, so feel free to edit my question and tags.

The detail implementation is a matter of taste, but key to do so is

• to find out the number of columns given in the data file with \pgfplotstablegetcolsof,
• store it in a variable with \pgfmathtruncatemacro and
• then loop through the file using one of the "foreach" commands, e.g. \pgfplotsinvokeforeach.

Because you didn't provide a full MWE yourself I used Zarko's answer as basis and modified it with the approach I already showed in this answer.

Something similar you can also find in the following answers

Hope that is sufficient for you for a good start.

% used PGFPlots v1.17
\begin{filecontents*}{data.csv}
date,backlog,wip,finished
2015-01-06,54,27,3
2015-01-13,55,27,5
2015-01-20,55,27,5
2015-01-27,54,27,8
2015-02-03,54,27,8
2015-02-10,56,27,10
2015-02-17,56,25,12
2015-02-24,63,24,17
2015-03-02,63,21,17
2015-03-09,59,23,20
2015-03-16,59,25,21
2015-03-23,55,27,26
2015-03-30,55,30,26
2015-04-06,62,28,30
2015-04-13,62,28,30
2015-04-20,65,22,40
2015-04-27,65,22,40
2015-05-04,61,22,44
2015-05-11,61,20,47
2015-05-18,60,21,50
2015-05-25,59,21,50
\end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.17}
% ----------------------------------------------------------------------
\newcommand{\plotfile}[1]{
\pgfplotstablegetcolsof{\table}
\pgfmathtruncatemacro\numberofcols{\pgfplotsretval-1}
\pgfplotsinvokeforeach{1,...,\numberofcols}{
\pgfplotstablegetcolumnnamebyindex{##1}\of{\table}\to{\colname}
\closedcycle;
}
}
% ----------------------------------------------------------------------
\begin{document}
\begin{tikzpicture}
\begin{axis}[
date coordinates in=x,
xticklabel={\day.\month.\year},
xticklabel style={
rotate=90,
anchor=near xticklabel,
},
xmin={2015-01-06},
xmax={2015-05-25},
ymin=0,
ymax=140,
max space between ticks=20,
stack plots=y,
area style,
]
\plotfile{data.csv}
\end{axis}
\end{tikzpicture}
\end{document}


• First of all: Thank you! But how can I have the first column as an index though? Dec 29, 2020 at 17:18
• You are welcome. To your question; there are two ways: 1. The best way is to (simply) change the data in the file. 2. Add x expr=\coordindex, before y index. Dec 29, 2020 at 18:35
• When I insert x expr=\coordindex, before y index, I get an error. Also... This is answer works with dates, but how do I use it with regular integers? Dec 29, 2020 at 19:01
• Of course you need to remove all the stuff related to "dates", i.e. the first three axis options. Dec 29, 2020 at 19:06
• What does the x expr=\coordindex, do? Dec 29, 2020 at 19:40