[Tex/LaTex] How to draw density estimate on histogram by pgfplots

pgfplots

I know how to draw a density histogram using pgfplots from this answer: Is it possible to "transform" a histogram into a density plot in pgfplots?.

But how can I draw an estimate line on the histogram like this?

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\pgfplotsset{
    small,
    every axis plot post/.style={
        fill=orange!75,
        draw=orange!50!black
    },
    trim axis left
}

\begin{tikzpicture}
\begin{axis}[ymin=0, title=\texttt{hist=density}]
\addplot [hist=density] table [y index=0] {random.txt};
\end{axis}
\end{tikzpicture}
\end{document}

Best Answer

You can use gnuplot to do the kernel density estimate:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[ymin=0]
\addplot [
    fill=orange!75,
    draw=orange!50!black,
    hist=density
] table [y index=0] {random.tsv};
\addplot [thick] gnuplot [raw gnuplot] {plot 'random.tsv' u 1:(1./1000.) smooth kdensity};
\end{axis}
\end{tikzpicture}
\end{document}
Related Question