The following density plots have been made using the same data. Only the bandwidth value changes from 1 in the first graph to 0.2 on the right. This is controlled using the bw_adjust argument of the kdeplot function (seaborn library).

This parameter can be of particular interest when a finer understanding of the distribution is needed. It could highlight bimodal distributions more easily and help us in observing patterns that the Gaussian kernel over-smoothed.

Note that in older version of seaborn (< 0.11.0), the bw parameter was used but is deprecated since and bw_method and bw_adjust have replaced it.

See scipy.stats.gaussian_kde in scipy.org for further details on bw_method and bw_value.

# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="darkgrid")
df = sns.load_dataset('iris')
 
# Large bandwidth
sns.kdeplot(df['sepal_width'], fill=True, color="olive")
plt.show()

In seaborn 0.11.0 and before versions, you would use sns.kdeplot(df\['sepal_width'\], shade=True, bw=0.05, color='olive')

Now, shade and bw arguments are deprecated.

# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="darkgrid")
df = sns.load_dataset('iris')

# Narrower bandwidth (if using seaborn > 0.11.0)
sns.kdeplot(df['sepal_width'], fill=True, bw_adjust=0.2, color='olive')
plt.show()

Contact & Edit


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!