06 Density Curves#

%%html
<iframe width="700" height="400" src="https://www.youtube.com/embed/PUvUQMQ7xQk/" frameborder="0" allowfullscreen></iframe>
import numpy as np 
import pandas as pd
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats

khanacademy

Towards Data Science - Histograms and Density Plots in Python

Density Curves fig 1Density Curves fig 2Density Curves fig 3Density Curves fig 4Density Curves fig 5Density Curves fig 6Density Curves fig 7

x = Series([0.5, 0.7, 2.1, 2.2, 2.9, 3.2, 3.2, 3.3, 3.7, 4.5, 4.6, 4.8, 5.2, 5.3, 6.7, 8.1])
x.plot(kind='hist')
<AxesSubplot:ylabel='Frequency'>
../_images/06 Density Curves_7_1.png
x.plot(kind='density')
<AxesSubplot:ylabel='Density'>
../_images/06 Density Curves_8_1.png
sns.distplot(x)
/opt/hostedtoolcache/Python/3.9.13/x64/lib/python3.9/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)
<AxesSubplot:ylabel='Density'>
../_images/06 Density Curves_9_2.png
# https://stackoverflow.com/questions/4150171/how-to-create-a-density-plot-in-matplotlib
density = stats.gaussian_kde(x)
xs = np.arange(0., 8, .1)
density.covariance_factor = lambda : .25 # to remove
density._compute_covariance() # to remove
plt.plot(xs,density(xs))
plt.show()
../_images/06 Density Curves_10_0.png