[Tex/LaTex] Plot multiple histograms from csv file using pgfplot

csvpgfplotstikz-pgf

I have the following data.csv file:

subject,f1,f2,f3
F11,0.019,0.04165,0.00016547
F14,0.03034,0.02161,0.000267
M22,0.05128,0.0648,0.000327
M22_1,0.052,0.0328,0.000206
M23,0.0364,0.06355,0.000379
F37,0.02856,0.081253,0.00019

and I would like to plot three histograms (f1,f2,f3) for each subject.
I am currently trying with this code:

\begin{tikzpicture}
 \begin{axis}[width=10cm, height=5cm,legend pos=outer north east,xlabel=ages,ylabel=values,ticks with fixed point,xtick=data,ybar,ymin=0,ymax=0.1,ytick={0,0.04,0.08}]
 \addplot [color=blue,fill] table [x expr=\coordindex, y={f1}, col sep=comma] {data.csv};
  \addplot [color=green,fill] table [x expr=\coordindex, y={f2}, col sep=comma] {data.csv};
  \addplot [color=red,fill] table [x expr=\coordindex, y={f3}, col sep=comma] {data.csv};
  \legend{f1,f2,f3}
 \end{axis}
\end{tikzpicture}

But subject ids (F11,…,F37) on the x axis do not appear. Moreover, I would like to move the legend on the bottom.

I found a similar question PGFplot multiple histograms using CSV file, but I did not understand how to solve my problem.

Best Answer

To get the tick labels from the table, use xticklabels from table={\mydata}{subject}, where \mydata is the table, and subject is the name of the column. To place the legend below, I used legend to name={label}, which lets you place the legend where you like with \ref{label}. I used a node placed relative to the axis. Note also that I added $ ... $ around the subscripted number in the subject column.

pgfplotstableread can take the raw values as input (as in my original answer), or the filename of a text file. To remove the scientific notation from the yticklabels, see e.g. pgfplot: accuracy of tick labels or Remove the scientific notation which is unreasonable.

enter image description here

\documentclass{article}
\usepackage{filecontents}
% the filecontents package and environment allows you to create a new file
% this is just to make the example self contained, I don't suppose you'll use it yourself
\begin{filecontents*}{data.dat}
subject,f1,f2,f3
F11,0.019,0.04165,0.00016547
F14,0.03034,0.02161,0.000267
M22,0.05128,0.0648,0.000327
M22$_1$,0.052,0.0328,0.000206
M23,0.0364,0.06355,0.000379
F37,0.02856,0.081253,0.00019
\end{filecontents*}

\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.12}
\pgfplotstableread[col sep=comma]{data.dat}\mydata

\begin{document}
\begin{tikzpicture}
\begin{axis}[width=10cm, height=5cm,
  legend style={legend columns=-1},
  legend to name={thelegend},
  name={theaxis},
  xlabel=ages,
  ylabel=values,
  xtick=data,
  xticklabels from table={\mydata}{subject},
  ybar,
  ymin=0,ymax=0.1,
  ytick={0,0.04,0.08},
  yticklabel style={
        /pgf/number format/fixed,
        /pgf/number format/precision=2
}]
\addplot [color=blue,fill] table [x expr=\coordindex, y={f1}] \mydata;
\addplot [color=green,fill] table [x expr=\coordindex, y={f2}] \mydata;
\addplot [color=red,fill] table [x expr=\coordindex, y={f3}] \mydata;
\legend{f1,f2,f3}
\end{axis}
\node [below] at (theaxis.below south) {\ref{thelegend}};
\end{tikzpicture}
\end{document}