# Flat line in Plot PDF of empirical data

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]


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.

• You can try EstimatedDistribution or FindDistribution if you want to try to get a continuous distribution for your data. Commented Sep 8, 2017 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]


## SmoothKernelDistribution

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

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


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.

## EstimatedDistribution

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}]


• Wow, thank you very much @rhermans for your extensive explanations! It was very helpful! Commented Sep 8, 2017 at 15:51
• @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! Commented Sep 8, 2017 at 20:18
• 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. :) Commented Sep 9, 2017 at 14:59