用于频率分析的 PGF 图

用于频率分析的 PGF 图

我想创建一个小型的 PGF 图来表示德语的字母频率。我尝试了很多次,但还是没能得到结果。以下是我目前所做的:

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

我的主要问题是,并非所有绘制的坐标都在绘图区域中。我尝试过,width = \textwidth但这只会产生更多问题。此外,x 轴上只有一个字母,而不是整个字母表,并且条形图之间有很大空间,我无法将其最小化。有人能帮我吗?

答案1

像这样:

在此处输入图片描述

你对foreach循环的使用是错误的。更好/正确的方法是将循环内容移动到坐标

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

如果您想比较不同语言的字母频率,那么您应该形成一个表格而不是坐标。

相关内容