🔵 Scatter Plot

Last Updated: 06 Nov 2025


Scatter plot is used to show the relationship between two variables.

Hinglish Tip 🗣: Scatter plot ko “do cheezon ke beech ka relation” samajhne ke liye use karte hain — jaise height vs weight.


✏ Basic Scatter Plot

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [5, 7, 6, 10, 8]

plt.scatter(x, y)
plt.show()

Useful Parameters for Scatter Plot

  • color - Color of the points
  • marker - Shape of the points
  • s - Size of the points
  • alpha - Transparency of the points
  • linewidths - Marker edge width
  • edgecolor - Marker edge color
  • cmap - Color map for the points
  • c - Color value for the each points
plt.figure(figsize=(8, 4))

plt.scatter(
    x,
    y,
    color="blue",         # dot color
    s=100,                # size
    alpha=0.7,            # transparency
    marker="o",           # circle marker
    edgecolor="black",    # border
    linewidths=1,         # border width
    label="Data Points"
)

plt.title("Scatter Plot Example")
plt.xlabel("X Values")
plt.ylabel("Y Values")
plt.legend()
plt.grid(True, linestyle="--", alpha=0.5)

plt.show()

Color-Coded Scatter Plot (Using c and cmap)

import numpy as np

x = np.random.rand(50)
y = np.random.rand(50)
colors = np.random.rand(50)     # color per point

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

plt.scatter(x, y, c=colors, cmap="viridis", s=80)

plt.colorbar(label="Intensity")
plt.title("Colored Scatter Plot")
plt.show()

Scatter Plot With Trend Line

import numpy as np

x = np.array([1,2,3,4,5])
y = np.array([2,4,5,4,5])

# scatter
plt.scatter(x, y, s=100)

# trend line
m, b = np.polyfit(x, y, 1)
plt.plot(x, m*x + b)

plt.show()

Multiple Scatter Plots in Same Chart

hours_a = [2,4,6,8,10]
scores_a = [30,50,65,80,90]

hours_b = [1,3,5,7,9]
scores_b = [25,45,60,75,85]

plt.scatter(hours_a, scores_a, color="steelblue", s=80, label="Class A")
plt.scatter(hours_b, scores_b, color="lightcoral", s=80, label="Class B")
plt.title("Study Hours vs Scores (Two Classes)")
plt.xlabel("Hours Studied")
plt.ylabel("Score")
plt.legend()
plt.grid(True)
plt.savefig("study_hours_scores_two_classes.png")
plt.show()