I am currently trying to plot the probability density function PDF of an empirical data set. However, unfortunately, I always receive just a flat line in the Plot

My code is as follows:

empdist = EmpiricalDistribution[data];
Plot[PDF[empdist , x], {x, 0, Max[data]}, PlotRange -> All]

Mathematica graphics

If I use the DiscretePlot function it works, however then there are lines to the points on the x-axis which I do not want as am fitting another distribution on top of it.

Mathematica graphics

  • 1
    $\begingroup$ You can try EstimatedDistribution or FindDistribution if you want to try to get a continuous distribution for your data. $\endgroup$ – N.J.Evans Sep 8 '17 at 15:18

For EmpiricalDistribution, don't use Plot but DiscretePlot

A PDF of EmpiricalDistribution of a finite set of data is necessarily a series of DiracDelta at each data point. Plot has problem with the discontinuity of that function. If you look at the documentation of EmpiricalDistribution you will see it uses DiscretePlot. Plotting that and seen an horizontal set of points may not look very meaningful to you but is correct. If you don't want Filling (the vertical lines) just set it to None.

ed = EmpiricalDistribution[data = RandomVariate[NormalDistribution[], 25]];
DiscretePlot[PDF[ed, x], {x, data}, Filling -> None]

Mathematica graphics


If you need a "Kernel" other than a DiracDelta so that points "add to another", then you should use SmoothKernelDistribution

 Evaluate@PDF[SmoothKernelDistribution[data], x]
 , {x, -2, 2}]

Mathematica graphics

This is related to HistogramDistribution, but continuous, and to SmoothHistogram. But beware that this is a parametric estimation, you need to chose a parameter in this case a bandwidth (even if the default is Automatic), and the distribution you get will depend on that choice. A good value could be the intrinsic uncertainty of your measurement.


On the other hand if you already know what the distribution is, but just need to define empirically the coefficients, then use EstimatedDistribution (as suggested by N.J.Evans)

ed = EstimatedDistribution[data, NormalDistribution[m, s]]
(* NormalDistribution[0.161522, 0.740823] *)

Plot[Evaluate@PDF[ed, x], {x, -2, 2}]

Mathematica graphics

  • 1
    $\begingroup$ Wow, thank you very much @rhermans for your extensive explanations! It was very helpful! $\endgroup$ – Alexander Hempfing Sep 8 '17 at 15:51
  • $\begingroup$ @AlexanderHempfing, I'm happy to help, and glad you appreciate it. There is a way to return the favour, not to me, but to the community. As you receive give back, vote and answer questions, keep the site useful, be kind, correct mistakes. Did something cool or pedagogical? Share what you have learned! You have posted 8 questions, time to offer an answer! $\endgroup$ – rhermans Sep 8 '17 at 20:18
  • 1
    $\begingroup$ You are right! Thank you for the suggestion. I am currently working on my thesis and will share some code - at least the interesting stuff - as soon as I am finished. And I will try to engage more in the community - at least if I am able to offer a solution to problems posted here. :) $\endgroup$ – Alexander Hempfing Sep 9 '17 at 14:59

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.