Distribution Plots (displot, histplot)
Why distribution plots matter
Section titled “Why distribution plots matter”Distribution plots answer:
- What values are most common?
- Is the data skewed?
- Are there outliers?
- Do groups behave differently?
flowchart LR A["Raw values"] --> B["Split into bins
(equal-width ranges)"] B --> C["Count values
per bin"] C --> D["Draw bars
(histplot)"] D --> E["Optional: overlay
a smooth KDE curve"]
histplot (single plot)
Section titled “histplot (single plot)”import seaborn as sns
import matplotlib.pyplot as plt
values = [55, 60, 62, 63, 65, 67, 70, 72, 76, 80, 81, 85, 90, 92, 95]
plt.figure(figsize=(7, 4))
sns.histplot(values, bins=8, kde=False)
plt.title("Histogram")
plt.xlabel("Value")
plt.ylabel("Count")
plt.tight_layout()
plt.show()Add KDE on top
Section titled “Add KDE on top”import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(7, 4))
sns.histplot(values, bins=8, kde=True)
plt.title("Histogram + KDE")
plt.tight_layout()
plt.show()Visualize it
Section titled “Visualize it”Bars count how many points land in each bin. The smooth curve is a KDE — an estimate of the underlying probability density that doesn’t depend on where the bin edges happen to fall:
displot (figure-level)
Section titled “displot (figure-level)”displot can create more complex plots and handles facets.
import seaborn as sns
# Seaborn includes sample datasets
tips = sns.load_dataset("tips")
sns.displot(data=tips, x="total_bill", bins=20, kde=True)Compare distributions by group
Section titled “Compare distributions by group”import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
plt.figure(figsize=(7, 4))
sns.histplot(data=tips, x="total_bill", hue="sex", bins=20, kde=True, element="step")
plt.title("Total bill distribution by sex")
plt.tight_layout()
plt.show()- Use the same bin settings when comparing groups.
- KDE is a smooth estimate; it can hide small details.
- For heavy outliers, consider log scaling.
Continue to Kernel Density Estimation (KDE) to look at the smooth curve on its own, and learn how bandwidth controls its shape.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – A Basic Histogram
Section titled “Exercise 1 – A Basic Histogram”Exercise 2 – Add a KDE Curve
Section titled “Exercise 2 – Add a KDE Curve”Exercise 3 – Compare Groups by Column
Section titled “Exercise 3 – Compare Groups by Column”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading