# How to extract the probability density function from SmoothHistogram? [duplicate]

I have some data.I plot the histogram with SmoothHistogram in Mathematica. If the y-axis of the histogram is my p(x) function, I want to plot f(x)=(1/n)ln(p(x)) with Mathematica. I don't know how?!

data = Import["Desktop/kmc final result.txt", "List"];
d = SmoothHistogram[data, Automatic, "PDF"];
time = 200;

f[x_] := -1/time Log[d, x]

• Welcome! Please add working code to demonstrate your problem. As it is, we cannot sensibly answer because of lack of information. Sep 4, 2014 at 8:37
• Data = Import["Desktop/kmc final result.txt", "List"]; d = SmoothHistogram[Data, Automatic, "PDF"]; time = 200; f[x_] := -1/time Log[d, x]; Sep 4, 2014 at 8:49

With some data

data = RandomVariate[NormalDistribution[], 1000]


the SmoothHistogram plot:

SmoothHistogram[data]


is using SmoothKernelDistribution.
You can create the same DataDistribution object using

dist = SmoothKernelDistribution[data]


and then extract the values you see in the plot at a specific single point e.g.

PDF[dist, 0.5]


0.347436

create a table of values, e.g.:

Table[{x, PDF[dist, x]}, {x, -3.5, 3.5, 0.1}]


or use it within Plot or any other suitable function:

time = 200;
f[x_] := -1/time Log[PDF[dist, x]]
Plot[f[x], {x, -4, 4}]


• So you mean that there is no difference between "smoothkerneldistribution" and "SmoothHistogram"?! Sep 4, 2014 at 8:52
• That is how I read the documentation: "SmoothHistogram[data] by default plots the PDF of [data], based on a smooth kernel density estimate. ... The specifications for bandwidth bw and kernel are the same as for SmoothKernelDistribution" Sep 4, 2014 at 8:57
• Thanx alot dear friend! Sep 4, 2014 at 9:03

As a an alternative, you can also extract the data out of the SmoothHistogram object. This way is a bit dirty and should be done carefully because the "magic index" 1,2,1,3,3,1 could be dependent on Mathematica version and other parameters. This is for v10:

data = RandomVariate[NormalDistribution[], 1000];
dataHist = SmoothHistogram[data]


now directly access the list of plotted points by dataHist[[1, 2, 1, 3, 3, 1]]

dataHist[[1, 2, 1, 3, 3, 1]] // ListLinePlot
{#[[1]], 1/1000*Log[#[[2]]]} & /@dataHist[[1, 2, 1, 3, 3, 1]] // ListLinePlot


• one small issue to be aware of , the plot range of SmoothHistogram seems to cut off the very ends of the tails. Sep 5, 2014 at 18:10