TikZ-PGF – PGF Plot for Frequency Analysis

alphabettikz-pgf

I wanted to created a small PGF Plot for the letter frequency of the german language. I tried a lot, but I was not able to create a result. Here is what I did so far:

\documentclass[a4paper, 11pt]{book} % A4 paper size and default 11pt font size
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\scriptsize
\begin{axis}[
ybar,
width = \textwidth,
ylabel={Prozentsatz},
symbolic x coords={a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
]
\foreach \x in {
% Data From Wikipedia, German, English, french, spanish etc.
(a,6.51) , %(a,8.167 ),  %||{{0}}7,636 %||12,53     
(b,1.89) , %(b,1.492 ),  %||{{0}}0,901 %||{{0}}1,42 
(c,3.06) , %(c,2.782 ),  %||{{0}}3,260 %||{{0}}4,68 
(d,5.08) , %(d,4.253 ),  %||{{0}}3,669 %||{{0}}5,86 
(e,17.40), %(e,12.702),  %||14,715     %||13,68     
(f,1.66) , %(f,2.228 ),  %||{{0}}1,066 %||{{0}}0,69 
(g,3.01) , %(g,2.015 ),  %||{{0}}0,866 %||{{0}}1,01 
(h,4.76) , %(h,6.094 ),  %||{{0}}0,737 %||{{0}}0,70 
(i,7.55) , %(i,6.966 ),  %||{{0}}7,529 %||{{0}}6,25 
(j,0.27) , %(j,0.153 ),  %||{{0}}0,545 %||{{0}}0,44 
(k,1.21) , %(k,0.772 ),  %||{{0}}0,049 %||{{0}}0,00 
(l,3.44) , %(l,4.025 ),  %||{{0}}5,456 %||{{0}}4,97 
(m,2.53) , %(m,2.406 ),  %||{{0}}2,968 %||{{0}}3,15 
(n,9.78) , %(n,6.749 ),  %||{{0}}7,095 %||{{0}}6,71 
(o,2.51) , %(o,7.507 ),  %||{{0}}5,378 %||{{0}}8,68 
(p,0.79) , %(p,1.929 ),  %||{{0}}3,021 %||{{0}}2,51 
(q,0.02) , %(q,0.095 ),  %||{{0}}1,362 %||{{0}}0,88 
(r,7.00) , %(r,5.987 ),  %||{{0}}6,553 %||{{0}}6,87 
(s,7.27) , %(s,6.327 ),  %||{{0}}7,948 %||{{0}}7,98 
(t,6.15) , %(t,9.056 ),  %||{{0}}7,244 %||{{0}}4,63 
(u,4.35) , %(u,2.758 ),  %||{{0}}6,311 %||{{0}}3,93 
(v,0.67) , %(v,0.978 ),  %||{{0}}1,628 %||{{0}}0,90 
(w,1.89) , %(w,2.360 ),  %||{{0}}0,114 %||{{0}}0,02 
(x,0.03) , %(x,0.150 ),  %||{{0}}0,387 %||{{0}}0,22 
(y,0.04) , %(y,1.974 ),  %||{{0}}0,308 %||{{0}}0,90 
(z,1.13) , %(z,0.074 ),  %||{{0}}0,136 %||{{0}}0,52 
}{
\addplot coordinates {\x};
}
\end{axis}
\end{tikzpicture}

\end{document}

My main problem is, that not all plotted coordinates are in the plot area. I tried it with width = \textwidth but that just created more problems. Also there is only one letter at the x-Axis, not the whole alphabet and there is a big space between the bars, which I was not able to minimize. Can someone help me?

Best Answer

Like this:

enter image description here

Your uses of foreach loop is wrong. Better/correct is to move loop contents to coordinates

\documentclass[a4paper, 11pt]{book} % A4 paper size and default 11pt font size
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\scriptsize
\begin{axis}[width=\linewidth,
        ybar = 1mm,
   bar width = 3mm,
enlarge x limits=0.04,
ylabel={Prozentsatz},
symbolic x coords={a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z},
xtick=data,
ymin=0,
nodes near coords,
nodes near coords style={rotate=90, anchor=west, font=\footnotesize},
]
\addplot coordinates {
(a,6.51)
(b,1.89) 
(c,3.06)  
(d,5.08) 
(e,17.40) 
(f,1.66) 
(g,3.01) 
(h,4.76)  
(i,7.55) 
(j,0.27)
(k,1.21)
(l,3.44)
(m,2.53)
(n,9.78)
(o,2.51)
(p,0.79)
(q,0.02)
(r,7.00)
(s,7.27)
(t,6.15)
(u,4.35)
(v,0.67)
(w,1.89)
(x,0.03)
(y,0.04)
(z,1.13)
};

\end{axis}
\end{tikfzpicture}

If you like to compare frequencies of letters for different languages, than instead of coordinates you should form a table.

Related Question