[Tex/LaTex] Space between bars, height and width of the plot

bar chartpgfplots

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

Best Answer

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}