4

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.

2

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}
| improve this answer | |
  • Thank you, that's great. Is there any way to use the file csv instead of writing down all the values? Moreover, I would prefer to have on the y axis numbers in the decimal format (0.04 and 0.08) instead of the exponential format because of space. – werty Aug 5 '15 at 8:19
  • 1
    @werty Of course, I just couldn't be bothered to make a new file -- just add the filename to pgfplotstableread instead of all the values. For the ticklabels, see e.g. Remove the scientific notation which is unreasonable I'll update my answer in a bit. – Torbjørn T. Aug 5 '15 at 8:40
  • Thank you. Just to more questions: 1) Is it possible to force the 0 in the yaxis to be exactly $0$ instead of $0.00$ in double precision as other numbers on the y axis? 2) Is it possible to draw the "trend" for each histogram as in this picture i.stack.imgur.com/mZKaD.png ? – werty Aug 5 '15 at 9:25
  • @werty 1) I don't see that behaviour, I get just 0 as you see in the screenshot. 2) Probably, but I don't know how. \addplot [sharp plot] table [x expr=\coordindex, y={f1}] \mydata; doesn't really work. I suggest you ask a new question about that problem. – Torbjørn T. Aug 5 '15 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.