Python Central HubSign in

Data Analytics with Python / Phase 6: Statistical Visualization with Seaborn

Distribution Plots (displot, histplot)

Visualize distributions using histplot and displot, control bins, and compare distributions across groups.

Why distribution plots matter

Distribution plots answer:

  • What values are most common?
  • Is the data skewed?
  • Are there outliers?
  • Do groups behave differently?
diagram How histplot builds a histogram mermaid
Values are sorted into equal-width bins, counted, then drawn as bars — with an optional KDE curve laid on top.

histplot (single plot)

Python
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

Python
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

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:

sketch Histogram bars with a KDE curve p5.js
Amber bars are the binned counts; the blue curve is the smooth density estimate drawn over them.

displot (figure-level)

displot can create more complex plots and handles facets.

Python
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

Python
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()

Tips

  • Use the same bin settings when comparing groups.
  • KDE is a smooth estimate; it can hide small details.
  • For heavy outliers, consider log scaling.

Check your understanding

quizCheck yourself
  1. What does bins=8 do in sns.histplot(values, bins=8)?

    Show answer

    B — Splits the range into 8 intervals and counts points in each — A histogram splits the data into evenly spaced bins and each bar counts the points in its bin.

  2. Which argument adds a smooth density curve over the histogram?

    Show answer

    D — kde=True — kde=True overlays a kernel density estimate. element="step" only changes how bars are drawn.

  3. What is the main difference between histplot and displot?

    Show answer

    A — displot is figure-level and supports facets such as col — displot is the figure-level function, so it can create facet grids, while histplot draws on a single axes.

  4. When comparing total_bill between groups with hue, what does the lesson recommend?

    Show answer

    B — Use the same bin settings — Using the same bin settings keeps the groups comparable. Different bins can change the apparent shape.

Next

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

Exercise 1 – A Basic Histogram

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Add a KDE Curve

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Compare Groups by Column

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did