Plotting probability density function by sample with matplotlib [closed] Plotting probability density function by sample with matplotlib [closed] python python

Plotting probability density function by sample with matplotlib [closed]


If you want to plot a distribution, and you know it, define it as a function, and plot it as so:

import numpy as npfrom matplotlib import pyplot as pltdef my_dist(x):    return np.exp(-x ** 2)x = np.arange(-100, 100)p = my_dist(x)plt.plot(x, p)plt.show()

If you don't have the exact distribution as an analytical function, perhaps you can generate a large sample, take a histogram and somehow smooth the data:

import numpy as npfrom scipy.interpolate import UnivariateSplinefrom matplotlib import pyplot as pltN = 1000n = N//10s = np.random.normal(size=N)   # generate your data sample with N elementsp, x = np.histogram(s, bins=n) # bin it into n = N//10 binsx = x[:-1] + (x[1] - x[0])/2   # convert bin edges to centersf = UnivariateSpline(x, p, s=n)plt.plot(x, f(x))plt.show()

You can increase or decrease s (smoothing factor) within the UnivariateSpline function call to increase or decrease smoothing. For example, using the two you get:dist to func


What you have to do is to use the gaussian_kde from the scipy.stats.kde package.

given your data you can do something like this:

from scipy.stats.kde import gaussian_kdefrom numpy import linspace# create fake datadata = randn(1000)# this create the kernel, given an array it will estimate the probability over that valueskde = gaussian_kde( data )# these are the values over wich your kernel will be evaluateddist_space = linspace( min(data), max(data), 100 )# plot the resultsplt.plot( dist_space, kde(dist_space) )

The kernel density can be configured at will and can handle N-dimensional data with ease.It will also avoid the spline distorsion that you can see in the plot given by askewchan.

enter image description here