Page4/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 usebins='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β¦