4/13
Bar Charts, Histograms & Scatter Plots · Page 2 of 2

Histograms & Scatter Plots

Histograms — Visualizing Distributions

A histogram shows the frequency distribution of a continuous variable:

ax.hist(data, bins=30, color="#8b5cf6", edgecolor="white", alpha=0.7)

Choosing Bin Count

  • Too few bins → hides detail
  • Too many bins → shows noise
  • Rule of thumb: bins = int(n**0.5) or use bins='auto'

Scatter Plots — Showing Correlations

ax.scatter(x, y,
    c=color_values,    # color by a third variable
    s=size_values,     # size by a fourth variable
    alpha=0.6,
    cmap='viridis')

Adding a Trend Line

import numpy as np
z = np.polyfit(x, y, 1)  # linear fit
p = np.poly1d(z)
ax.plot(sorted(x), p(sorted(x)), "--", color="red", linewidth=1.5)

💡 Scatter plots are how you detect correlation — a key step before running regression models.

main.py
Loading...
OUTPUT
▶Click "Run Code" to execute…