I have the following data.csv file:


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

 \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};

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.


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

% 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

\pgfplotstableread[col sep=comma]{data.dat}\mydata

\begin{axis}[width=10cm, height=5cm,
  legend style={legend columns=-1},
  legend to name={thelegend},
  xticklabels from table={\mydata}{subject},
  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;
\node [below] at (theaxis.below south) {\ref{thelegend}};
| 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.