Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I find a function that fits the plot from the SmoothHistogram function?

For instance if I have data like this and plot a Smooth Histogram, how can I get a function that fits the plot?

data = {4, 4, 4, 4, 1, 1, 4, 1, 5, 5, 5, 5, 5};


share|improve this question
up vote 4 down vote accepted

This is one approach yielding an interpolating function:

  data = {4, 4, 4, 4, 1, 1, 4, 1, 5, 5, 5, 5, 5};
  f = Interpolation[
     DeleteDuplicates[(List @@ 
             Cases[ SmoothHistogram[data] , _Line, 
                      Infinity]), #1[[1]] == #2[[1]] &]]

  Plot[f[x], {x, 0, 6.5}]

enter image description here

share|improve this answer
I appreciate the accept, buy @szabolcs answer really is the best. – george2079 Jun 2 '14 at 13:12

SmoothHistogram uses kernel density estimation. So the correct and direct way to obtain the function is to construct a SmoothKernelDistribution, then take its PDF.

distr = SmoothKernelDistribution[data]

Plot[PDF[distr, x], {x, 0, 6}]

enter image description here

share|improve this answer
What is an actual function though? For instance, it looks like I could fit a polynomial to this curve. I want to know the actual best fitting function, not just recreate the plot via some other means. – Kane May 30 '14 at 18:21
@Kane As I said in the answer, these functions use kernel density estimation. Look up this term to find out what this function is precisely. There are several ways to do kernel density estimation. I don't know what Mathematica's defaults are, but you have full control over the details (i.e. the form of the kernel and the bandwidth) using the documented options. Mathematica uses interpolation to speed up computing these functions, so you won't get a symbolic formula for it (this wouldn't be of much use anyway). You can compute the function values numerically the way I showed. – Szabolcs May 30 '14 at 18:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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