# Plot bar chart from csv with different styles depending on row in csv

I try to plot the content of a csv file in a bar chart, where depending on the row the bar should have a different style. This is what I came up with so far:

\documentclass{article}
\usepackage{tikz}
\usepackage{filecontents}
\usepackage{pgfplotstable}
\usetikzlibrary{patterns}

\begin{filecontents*}{data.csv}
;res
a;1
b;2
c;3
d;4
\end{filecontents*}

\begin{document}
\begin{figure}
\center
\begin{tikzpicture}
\begin{axis}[xticklabels from table = {\datatable}{0}, xtick=data]
[x expr=\coordindex,
col sep=semicolon,
restrict expr to domain={\coordindex}{0:1},
y={res}] {\datatable};
fill=blue!50, postaction={
pattern=north east lines
}] table [x expr=\coordindex,
col sep=semicolon,
y={res},
restrict expr to domain={\coordindex}{2:3}] {data.csv};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}


My problem is that in this case the labels on the x axis are missing for the bars with patterns. Does anyone know, how to solve this?

• Welcome to TeX.SE! I guess you could e.g. use this post. The question is on some additional feature, but as far as I can see the code manages to add the x labels and does the individual styles somewhat more systematically. You could add your styles either to the loop or to the cycle list. – user121799 Nov 10 '18 at 17:17
• xtick=data means "add xticks at the data points from the first \addplot". In the question @marmot links to the tick positions are specified with xtick={0,...,3} instead of xtick=data. – Torbjørn T. Nov 10 '18 at 18:32
• @TorbjørnT. This is true but not the reason why I was linking the answer. Rather, I was linking it because of select row, which is IMHO slightly more elegant. All one has to do is to use that loop and adjust it to the needs. – user121799 Nov 10 '18 at 18:49
• @marmot Sure, but the specific question here was about why the xticklabels were gone, so I thought I'd point out exactly what caused that. – Torbjørn T. Nov 10 '18 at 18:54
• @TorbjørnT. Yes, you're right. – user121799 Nov 10 '18 at 18:55

All relevant information is already contained in Torbjørn T.'s comment and I will be happy to remove this post if Torbjørn adds his answer. All I did is to retrieve the number of rows from the table and add this information at the appropriated place.

\documentclass{article}
\usepackage{tikz}
\usepackage{filecontents}
\usepackage{pgfplotstable}
\usetikzlibrary{patterns}

\begin{filecontents*}{data.csv}
;res
a;1
b;2
c;3
d;4
\end{filecontents*}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\rownum}{\pgfplotsretval-1}
\typeout{\rownum}
\begin{axis}[xticklabels from table = {\datatable}{0},
xtick={0,...,\rownum}]
[x expr=\coordindex,
col sep=semicolon,
restrict expr to domain={\coordindex}{0:1},
y={res}] {\datatable};