Histograms, Bins & Probability Density0%
Histograms, Bins & Probability Density
Beginner12 min read•Updated: Jul 11, 2026
Histograms, Bins & Frequency Distributions
Reading Time: 8 Mins | Level: Intermediate
1. What is a Histogram?
A Histogram visualizes the distribution of a continuous numerical variable by dividing the data range into equal-width intervals called bins and counting how many data observations fall into each bin.
Difference from Bar Chart: A bar chart compares discrete categories (like Cities). A histogram illustrates the spread, skewness, and frequency density of a continuous numeric variable (like Employee Ages or Salaries).
2. Plotting Histograms with Custom Bins & KDE
Python
import matplotlib.pyplot as plt
import numpy as np
# Generate 1,000 synthetic transaction amounts
np.random.seed(42)
transaction_amounts = np.random.gamma(shape=3, scale=500, size=1000)
fig, ax = plt.subplots(figsize=(8, 5))
# bins=30 creates 30 bins; density=True normalizes counts to probability density
n, bins, patches = ax.hist(
transaction_amounts,
bins=30,
color='#3b82f6',
edgecolor='#1e293b',
alpha=0.75
)
ax.set_title('Transaction Value Frequency Distribution')
ax.set_xlabel('Transaction Amount (INR)')
ax.set_ylabel('Frequency (Transaction Count)')
ax.axvline(np.median(transaction_amounts), color='red', linestyle='--', label=f'Median: ₹{np.median(transaction_amounts):.0f}')
ax.legend()
plt.show()
Interactive Quiz
Multiple Choice Questions
Question 1
What is the key distinction between a bar chart and a histogram?
Question 2
What happens to a histogram if you choose too few bins (e.g.
bins=2)?Question 3
What does setting
density=True in ax.hist() accomplish?Question 4
Which parameter in
ax.hist() adds a visible border between neighboring bars to improve visual clarity?Question 5
What does
ax.axvline(x=500) draw on a plot?Next Lesson
Seaborn Themes, Palettes & Aesthetics
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Bar Charts, Horizontal Bars & Stacked Bars | Seaborn Themes, Palettes & Aesthetics |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.