我想将直方图与概率密度函数结合起来。按照 Jake 的建议
我现在的计划是将两者结合在一个图中,就像下面来自 R 示例的那样蔡或者一个来自 Dirk:
我不需要拟合分布,我已经使用提取了估计值易适配。我只想和拟合函数一起画出直方图gamma(1.7435, 21.263)
。
我读过gnuplot
例子,同样来自 Jake,但无法确定这是否是唯一的解决方案。特别是如果我已经有了这个功能。
你能帮我吗?
平均能量损失- 我目前拥有的:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{subcaption}
\begin{document}
\begin{figure}[htbp]
\centering
\begin{subfigure}{0.35\textwidth}
\centering
\begin{tikzpicture}
\usepgfplotslibrary{statistics}
\begin{axis}[
axis x line=middle,
axis y line=middle,
ybar,
ymin=0
]
\addplot +[hist] table [y index=0, col sep=comma,row sep=newline] {
data
48
51
48
51
66
0
0
25
31
60
9
43
49
15
22
1
};
\end{axis}
\end{tikzpicture}
\subcaption[subfigcapskip = 50pt]{Histogram}
\label{fig:histogram}
\end{subfigure}%
\hfill
\begin{subfigure}{0.35\textwidth}
\centering
\begin{tikzpicture}[
declare function={gamma(\z)=
2.506628274631*sqrt(1/\z)+ 0.20888568*(1/\z)^(1.5)+ 0.00870357*(1/\z)^(2.5)- (174.2106599*(1/\z)^(3.5))/25920- (715.6423511*(1/\z)^(4.5))/1244160)*exp((-ln(1/\z)-1)*\z;},
declare function={gammapdf(\x,\k,\theta) = 1/(\theta^\k)*1/(gamma(\k))*\x^(\k-1)*exp(-\x/\theta);}
]
\begin{axis}[
axis lines=left,
]
\addplot [smooth, domain=0:66, red] {gammapdf(x,1.7435, 21.263)};
\end{axis}
\end{tikzpicture}
\subcaption[subfigcapskip = 50pt]{Plot}
\label{fig:plot}
\end{subfigure}
\end{figure}
\end{document}
编辑-最终解决方案,感谢热心学生!
\documentclass[margin=1mm]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{tikzpicture}[
declare function={gamma(\z)= 2.506628274631*sqrt(1/\z)+ 0.20888568*(1/\z)^(1.5)+ 0.00870357*(1/\z)^(2.5)- (174.2106599*(1/\z)^(3.5))/25920- (715.6423511*(1/\z)^(4.5))/1244160)*exp((-ln(1/\z)-1)*\z;},
declare function={gammapdf(\x,\k,\theta) = 1/(\theta^\k)*1/(gamma(\k))*\x^(\k-1)*exp(-\x/\theta);},
declare function={cauchypdf(\x,\mu,\gamma) = 1/(pi*\gamma*(1+((\x-\mu)/\gamma)^2));}
]
\pgfplotsset{set layers}
\begin{axis}[
axis y line*=left,
ylabel=Histogram,
every axis y label/.style={rotate=90, blue, at={(-0.15,0.5)},},
ybar,
ymin=0,
yticklabel style={blue}
]
\addplot +[hist] table [y index=0, col sep=comma,row sep=newline] {
data
48
51
48
51
66
0
0
25
31
60
9
43
49
15
22
1
};
\end{axis}
\begin{axis}[
ylabel style={black},
axis y line*=right,
axis x line=none,
ylabel=PDF's,
every axis y label/.style={rotate=-90, black, at={(1.15,0.5)},},
/pgfplots/every y tick scale label/.style={
at={(1,1)},
above right,
inner sep=0pt,
yshift=0.3em,
},
yticklabel style={black},
legend entries={
$\textbf{Gamma}~\alpha=1.7435~ \beta=21.263$,
$~\textbf{Cauchy}~\sigma=16.1340~ \mu=39.554$
},
legend style={at={(1,-0.15)}},
ymin=0,
]
\addplot [thick, smooth, domain=0:66, black, mark=square] {gammapdf(x,1.7435, 21.263)};
\addplot [thick, smooth, domain=0:66, red, mark=triangle] {cauchypdf(x,39.554, 16.134)};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
\documentclass[margin=1mm]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\begin{filecontents}{data.csv}
data
48
51
48
51
66
0
0
25
31
60
9
43
49
15
22
1
\end{filecontents}
\begin{document}
\begin{tikzpicture}[
declare function={gamma(\z)=
2.506628274631*sqrt(1/\z)+ 0.20888568*(1/\z)^(1.5)+ 0.00870357*(1/\z)^(2.5)- (174.2106599*(1/\z)^(3.5))/25920- (715.6423511*(1/\z)^(4.5))/1244160)*exp((-ln(1/\z)-1)*\z;},
declare function={gammapdf(\x,\k,\theta) = 1/(\theta^\k)*1/(gamma(\k))*\x^(\k-1)*exp(-\x/\theta);},
]
\pgfplotsset{set layers}
\begin{axis}[
axis y line*=left,
ylabel=Histogram,
every axis y label/.style={rotate=90, blue, at={(-0.15,0.5)},},
ybar,
ymin=0,
yticklabel style={blue}
]
\addplot +[hist] table [y index=0, col sep=comma,row sep=newline] {data.csv};
\end{axis}
\begin{axis}[
ylabel style={red},
axis y line*=right,
axis x line=none,
ylabel=Function Plot,
every axis y label/.style={rotate=-90, red, at={(1.15,0.5)},},
/pgfplots/every y tick scale label/.style={
at={(1,1)},above right,inner sep=0pt,yshift=0.3em},
yticklabel style={red}
]
\addplot [smooth, domain=0:66, red] {gammapdf(x,1.7435, 21.263)};
\end{axis}
\end{tikzpicture}
\end{document}