Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm using pgfplots to generate bar plots from data files. Here's MWE:

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  xbar, 
%  enlarge y limits=0.2, 
  xlabel={Häufigkeit},
  symbolic y coords={sehr viel mehr,deutlich mehr,Verzehr ist gleich geblieben,etwas
    weniger O+G,deutlich weniger O+G,keine Bewertung möglich},
  ytick=data,
  nodes near coords, nodes near coords align={horizontal},
  ]
\addplot table[col sep=tab,header=false] {data1.dat};
\end{axis}
\end{tikzpicture}


\begin{tikzpicture}
\begin{axis}[
  xbar, 
%  enlarge y limits=auto, 
  xlabel={Häufigkeit},
  symbolic y coords={Äpfel,Melonen,Erdbeeren,Bananen,Trauben,Nektarinen,Aprikosen,Clementinen,Pfirsiche,Birnen,Kiwi,Pflaumen,individuell völlig verschieden,eigentlich alle Sorten,Orangen,Kirschen,Himbeeren,Ananas,Mirabellen},
  ytick=data,
  nodes near coords, nodes near coords align={horizontal},
  ]
\addplot table[col sep=tab,header=false] {data2.dat};
\end{axis}
\end{tikzpicture}
\end{document}

data1.dat:

11  sehr viel mehr
55  deutlich mehr
4   Verzehr ist gleich geblieben
0   etwas weniger O+G
0   deutlich weniger O+G
0   keine Bewertung möglich

data2.dat:

56  Äpfel
20  Melonen
12  Erdbeeren
45  Bananen
18  Trauben
12  Nektarinen
8   Aprikosen
4   Clementinen
10  Pfirsiche
19  Birnen
16  Kiwi
7   Pflaumen
1   individuell völlig verschieden
1   eigentlich alle Sorten
7   Orangen
3   Kirschen
1   Himbeeren
1   Ananas
1   Mirabellen

Here are the plots:

Bar plot 1

Bar plot 2

There are some problems I can not solve:

  1. I wonder why each plot has the same height and width even if more space is needed.
  2. How do I add space between bars in the second plot? Since I have many plots I'd like to have a global option that automatically adapts the space between bars.

Thanks in advance! Christoph

Edit: Here's plot that shows further problems with spacing (same pgfplots settings as in MWE except that I added “yticklabel style={text width=3cm,align=right}”, and different data, of course.)

enter image description here

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

By default, plots always take up the same amount of space (240pt by 207pt). If you want the plots to adjust their height depending on the number of bars, you can define the length of a unit in the y direction, by setting y=0.5cm, for instance. If you then define the bar width to be 0.4cm, there will be no overlap between the bars. Since PGFPlots version 1.7 (I think), you can also define the extra space at the top and bottom in absolute lengths (before that, you could only specify it in data units).

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  xbar, 
  y=-0.5cm,
  bar width=0.3cm,
  enlarge y limits={abs=0.45cm},
  xlabel={Häufigkeit},
  symbolic y coords={sehr viel mehr,deutlich mehr,Verzehr ist gleich geblieben,etwas
    weniger O+G,deutlich weniger O+G,keine Bewertung möglich},
  ytick=data,
  nodes near coords, nodes near coords align={horizontal},
  ]
\addplot table[col sep=comma,header=false] {
11,sehr viel mehr
55,deutlich mehr
4,Verzehr ist gleich geblieben
0,etwas weniger O+G
0,deutlich weniger O+G
0,keine Bewertung möglich
};
\end{axis}
\end{tikzpicture}


\begin{tikzpicture}
\begin{axis}[
  xbar,
  y=-0.5cm,
  bar width=0.3cm,
  enlarge y limits={abs=0.45cm},
%  enlarge y limits=auto, 
  xlabel={Häufigkeit},
  symbolic y coords={Äpfel,Melonen,Erdbeeren,Bananen,Trauben,Nektarinen,Aprikosen,Clementinen,Pfirsiche,Birnen,Kiwi,Pflaumen,individuell völlig verschieden,eigentlich alle Sorten,Orangen,Kirschen,Himbeeren,Ananas,Mirabellen},
  ytick=data,
  nodes near coords, nodes near coords align={horizontal},
  ]
\addplot table[col sep=comma,header=false] {
56,Äpfel
20,  Melonen
12,  Erdbeeren
45,  Bananen
18,  Trauben
12,  Nektarinen
8,   Aprikosen
4,   Clementinen
10,  Pfirsiche
19,  Birnen
16,  Kiwi
7,   Pflaumen
1,   individuell völlig verschieden
1,   eigentlich alle Sorten
7,   Orangen
3,   Kirschen
1,   Himbeeren
1,   Ananas
1,   Mirabellen
};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
Thank you so much! Your answer helped me a lot! But now occurs another problem with long yticklabels. Linespread is bigger than space between labels. AFAICS I have to ask a new question (to be able to post a MWE). –  typokurz Nov 25 '12 at 19:36
    
@typokurz: You can put font={\linespace{0}\selectfont} in your yticklabel style options to remove the leading from your labels. –  Jake Nov 25 '12 at 19:52
1  
@typokurz: I saw that you posted a couple of questions to the pgfplots mailing list. May I suggest you post them here as well? I find that they're more easily found by others if they're on this site than on the mailing list. –  Jake Nov 25 '12 at 19:56
    
Ok, I'll do it. Usually I try to prevent crossposts. I just made an exception to this with this question because I have a deadline … (why is “@Jake:” always removed again?) –  typokurz Nov 25 '12 at 20:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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