# Unable to read in text labels for pgfplots bar chart

I've started using pgfplots and can plot scatter plots where all data is numeric, but I'm having no luck at all with bar charts where the xlabel is text. I've tried to find a solution in the manual and here, but nothing seems to work. I'm trying to get a stacked ybar with each bar representing a yearly quarter, with the bar made up of different contributions, but in desperation I would be happy to just get the first column of data plotted. The following is where I have got too, but I get errors like "The requested list entry with index 1 of \datatable is too large; this list has not enough elements..". Can anyone show me a way forward please. The commented line was also tried.

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.9}

\begin{document}
Year,Electricity,Stationary,Transport,Fugitive,Industrial,Agriculture,Waste
September2003,185.9,78.5,76.3,33.8,30.6,89.3,13.3
September2004,195.2,79.4,79.1,34.2,31.2,89.1,13.0
September2005,194.4,81.8,79.5,35.6,31.1,88.5,12.8
September2006,200.0,81.3,81.2,36.8,31.5,88.7,12.7
September2007,202.4,83.2,83.7,39.4,33.4,86.6,13.0
September2008,204.3,85.2,85.1,38.0,33.9,86.0,13.3
September2009,201.0,84.1,84.9,39.0,30.5,83.3,13.3
September2010,198.6,89.1,86.0,39.4,34.6,82.4,13.3
September2011,192.4,90.1,89.1,38.8,34.0,85.3,12.7
September2012,189.0,90.7,90.9,41.4,30.4,88.8,12.7
September2013,178.7,92.2,92.8,44.8,30.5,90.4,12.6
}\datatable;

\begin{tikzpicture}
\begin{axis}[
ybar,
ymin=0,
ytick=data,
symbolic x coords={year 1, year 2},
]
%\addplot table[x expr=\thisrowno{0}, y expr=\thisrowno{1}*-1] {\datatable};
\end{axis}
\end{tikzpicture}%
\end{document}


Would this be what you want? To plot a stacked ybar, replace ybar by ybar stacked and add more addplot commands with y=stationary replaced, etc etc. Also remove ymax=210 or reset it. Or in a cleaner way automatically as shown in the second plot.

\foreach \i in {Electricity,Stationary,Transport,Fugitive,Industrial,Agriculture,Waste}{%
\addplot table [x expr=\coordindex, y=\i] {\datatable};}


Code:

\documentclass[border=2cm]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.8}

\begin{document}

Year,Electricity,Stationary,Transport,Fugitive,Industrial,Agriculture,Waste
September2003,185.9,78.5,76.3,33.8,30.6,89.3,13.3
September2004,195.2,79.4,79.1,34.2,31.2,89.1,13.0
September2005,194.4,81.8,79.5,35.6,31.1,88.5,12.8
September2006,200.0,81.3,81.2,36.8,31.5,88.7,12.7
September2007,202.4,83.2,83.7,39.4,33.4,86.6,13.0
September2008,204.3,85.2,85.1,38.0,33.9,86.0,13.3
September2009,201.0,84.1,84.9,39.0,30.5,83.3,13.3
September2010,198.6,89.1,86.0,39.4,34.6,82.4,13.3
September2011,192.4,90.1,89.1,38.8,34.0,85.3,12.7
September2012,189.0,90.7,90.9,41.4,30.4,88.8,12.7
September2013,178.7,92.2,92.8,44.8,30.5,90.4,12.6
}\datatable

\begin{tikzpicture}
\begin{axis}[
ybar,
ymin=0,
ymax=210,
xtick=data,
xticklabels from table={\datatable}{Year},
x tick label style={rotate=90, anchor=east}
%%symbolic x coords={year 1, year 2, year 3, year 4, year 5, year 6, year 7, year 8, year 9, year 10, year 11},
]
\addplot table [x expr=\coordindex, y=Electricity] {\datatable};
\end{axis}
\end{tikzpicture}%
\end{document}

• Yes that's it, and I've been able to do a stacked plot as well, thanks very much. Out of interest is it easy to loop through the columns to avoid manually adding 7 addplots? Mar 8 '14 at 22:19
• Yes, please see my update. Mar 9 '14 at 0:18