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:
There are some problems I can not solve:
- I wonder why each plot has the same height and width even if more space is needed.
- 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.)
Best Answer
By default, plots always take up the same amount of space (
240pt
by207pt
). If you want the plots to adjust their height depending on the number of bars, you can define the length of a unit in they
direction, by settingy=0.5cm
, for instance. If you then define thebar width
to be0.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).