8

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}
  • 5
    Could you post an MWE of what you have tried so far (at least something people can work with and expand)? Your question leaves all the effort to our community, even typing the essentials of a TeX document such as \documentclass{}...\begin{document} etc. As it is, most of our users will be very reluctant to touch your question, and you are left to the mercy of our procrastination team who are very few in number and very picky about selecting questions. You can improve your question by adding a minimal working example (MWE) that more users can copy/paste onto their systems to work on. – moewe Feb 17 '14 at 7:38
  • 1
    The smooth histogram depends on a certain Kernel and a bandwidth. I would suggest to use an external programm for the calculation and import the data. Using knitr (and R) or a shell command from within latex could be helpful. – jens_bo Feb 17 '14 at 10:12
7

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}
  • 3
    How did you run it? I don't know how to work with pgfplots codes which include gnuplot. – Enthusiastic Engineer Nov 10 '14 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.