pgfplots 带曲线的直方图

pgfplots 带曲线的直方图

这个问题的变体以前曾被问过很多次,但我找不到可行的解决方案。我见过的最接近的是其他 stackexchange 上关于如何使用 R 生成此问题的讨论。

我正在尝试制作以下带有红线和蓝线的图(线的位置是仅是估计我希望它们出现在解决方案中的位置,解决方案必须根据 data.csv 文件中的信息计算它们的位置):

例子

我提出这个问题的部分困难在于,我不确定用什么术语来描述我在图表上画的红线和蓝线。我不知道思考它们是高斯​​曲线,因为它们不对称,但尽管如此我在下面的 MWE 中包含了高斯代码,以防我的实现有误(上图左下方有一个非常非常小的高斯图,我显然没有成功实现)。

编辑:正如下面的评论所讨论的,我正在寻找曲线来展示结果分布的平滑估计。

平均能量损失

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{document}

\begin{filecontents*}{data.csv}
COLUMNA,COLUMNB
38,22
85,18
104,82
56,20
202,57
64,15
115,22
8,20
120,14
81,24
100,28
39,11
81,29
25,18
122,51
93,10
45,19
103,11
33,24
60,24
50,47
61,24
46,14
45,15
84,72
62,20
50,13
84,38
52,19
108,5
182,34
145,19
117,12
34,59
43,19
42,26
170,18
31,27
86,18
183,24
36,15
,21
,16
,26
\end{filecontents*} 

\pgfmathdeclarefunction{gauss}{2}{\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

\begin{tikzpicture}
\centering
\begin{axis}[
ybar,
/pgf/number format/.cd,
use comma,
1000 sep={},
title={Title},
xlabel={Bins},
ylabel={Instances},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:0.05,0.5)},anchor=south},
%xticklabel style={rotate=90, anchor=near xticklabel},
xtick distance=50,
ytick distance=2,
width=\textwidth, %10.5cm
height=6cm,
axis y line*=left,
axis x line*=bottom,
ymin=0,
xmin=0,
xticklabel interval boundaries,
]

%%%
\addplot +[blue,
fill opacity=0.5,
hist={bins=22,
data min=0,
data max=220,
}
] table[y=COLUMNA, col sep=comma] {data.csv};
\addlegendentry{Series A}

\addplot +[red,
fill opacity=0.5,
hist={bins=22,
data min=0,
data max=220,
}
] table[y=COLUMNB, col sep=comma] {data.csv};
\addlegendentry{Series A}

\addplot [fill=red!50, draw=none, domain=0:220] {gauss(1.86,2.12)};

\end{axis}
\end{tikzpicture}
\end{document}

相关内容