MatplotlibXBy @CodeWithMunnaX
Catalog
Track: Essential Charts
Statistical Charts12 mins

13. Histograms & Probability Distributions

A histogram bins continuous numerical data into discrete intervals and counts how many observations fall into each bin, revealing central tendency, skewness, spread, and multi-modal distributions.

Mental Model Intuition

Sorting coins through a coin separator into specific slot buckets based on diameter.

Core Concepts & Mechanics

Choosing Bin Count (`bins`)Resolution

`bins=10` creates 10 equal-width buckets. You can also pass explicit bin edges `bins=[0, 50, 75, 100, 150]`.

Too few bins oversmoothes the data (hiding sub-peaks); too many bins creates jagged noise.
plt.hist(data, bins=20)
Probability Density Normalization (`density=True`)Statistics

Setting `density=True` normalizes bin heights so the total area under the histogram integrates to 1.0.

Use `density=True` when comparing distributions with different sample sizes.
plt.hist(data, bins=15, density=True)

Common Beginner Mistakes & Pro Fixes

Avoid the top errors and bad plotting practices that trip up new Python developers

Troubleshooting

Quick Lesson Takeaways

  • `plt.hist(data, bins=N)` groups raw continuous data into frequency bins.
  • `edgecolor` outlines individual bins for visual separation.
  • `density=True` normalizes frequencies into probability density.
Topic 13 • Python IDE
Python 3.12 • Matplotlib
Run Python code to inspect terminal output.

Real-World Datasets (1-Click Switcher)

Switch from abstract numbers to relatable Cricket, Cinema & Business charts

5 Curated Datasets

Hands-on Challenge: Plot a 10-Bin Histogram

+50 XP

Write Matplotlib code to solve the objective and see your live plot render

Objective Instructions:

Plot a histogram of `data = [12, 15, 18, 22, 25, 29, 31, 35, 40, 42, 48, 50]` with `bins=6` and `color='#FFB86B'`.

Required Keywords:✓ plt.hist✓ bins
Challenge Code Editor
Challenge Output GraphLive Vector Preview
Write code on the left to render live challenge plot
End of Lesson Knowledge Check

Quick Mini Quiz: 2 Concept Questions

Test your understanding to unlock the lesson completion badge

Q1

Which Matplotlib function is used to create a standard continuous 2D line plot?

Q2

What must you call after specifying label='...' so that the legend actually appears on the canvas?

0 of 2 answered