06 Density Curves
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
Towards Data Science - Histograms and Density Plots in Python
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'>
x.plot(kind='density')
<AxesSubplot:ylabel='Density'>
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'>
# 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()