📚 Histogram

Last Updated: 06 Nov 2025


Histogram is used to show distribution of data. It helps you understand data spread, frequency, and patterns.

Hinglish Tip 🗣: Histogram ko “data kitna spread hai” dekhne ke liye use karte hain — jaise marks distribution.


Basic Histogram

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)

plt.hist(data)
plt.show()

Useful Parameters for Histogram

  • bins - Number of bins in the Histogram
  • alpha - Transparency of the Histogram
  • color - Color of the Histogram
  • edgecolor - Color of the edges of the Histogram
  • density - Normalize the Histogram to a probability density
  • histtype - Type of Histogram to plot("bar", "barstacked", "step", "stepfilled")
  • orientation - Orientation of the Histogram("vertical", "horizontal")
import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)

plt.figure(figsize=(8, 4))

plt.hist(
    data,
    bins=20,                   # number of bars
    color="skyblue",           # bar color
    edgecolor="black",         # bar outline
    alpha=0.8,                 # transparency
    histtype="bar",            # bar/step
    label="Data Dist"
)

plt.title("Histogram Example")
plt.xlabel("Values")
plt.ylabel("Frequency")
plt.legend()
plt.grid(axis="y", linestyle="--", alpha=0.5)

plt.show()

Density Histogram

Used to show probability instead of counts.

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(1000)

plt.figure(figsize=(8, 4))

plt.hist(data, bins=20, density=True, edgecolor="black")
plt.title("Density Histogram")
plt.show()

Horizontal Histogram

plt.hist(data, bins=15, orientation="horizontal", edgecolor="black")
plt.title("Horizontal Histogram")
plt.show()

Multiple Histograms in One Plot

data1 = np.random.randn(500)
data2 = np.random.randn(500) + 2   # shifted mean

plt.hist(data1, bins=20, alpha=0.5, label="Group A")
plt.hist(data2, bins=20, alpha=0.5, label="Group B")

plt.legend()
plt.show()

Histogram With Normal Curve

from scipy.stats import norm

mu, sigma = np.mean(data), np.std(data)

plt.hist(data, bins=20, density=True, alpha=0.6, edgecolor="black")

xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 100)
pdf = norm.pdf(x, mu, sigma)

plt.plot(x, pdf)     # normal curve

plt.show()