0
0
Matplotlibdata~5 mins

Basic scatter plot with plt.scatter in Matplotlib

Choose your learning style9 modes available
Introduction

A scatter plot helps you see how two sets of numbers relate to each other by showing points on a graph.

To check if there is a pattern between hours studied and exam scores.
To see how temperature changes with time during a day.
To compare sales numbers of two products over several months.
To visualize the relationship between age and height in children.
Syntax
Matplotlib
plt.scatter(x, y, color='blue', marker='o')

x and y are lists or arrays of numbers of the same length.

You can change the color and shape of points using color and marker.

Examples
Basic scatter plot with default blue circles.
Matplotlib
plt.scatter([1, 2, 3], [4, 5, 6])
Scatter plot with red points.
Matplotlib
plt.scatter([1, 2, 3], [4, 5, 6], color='red')
Scatter plot with cross-shaped points.
Matplotlib
plt.scatter([1, 2, 3], [4, 5, 6], marker='x')
Sample Program

This program creates a scatter plot showing how exam scores increase with hours studied. Points are green circles. Labels and a grid help read the plot.

Matplotlib
import matplotlib.pyplot as plt

# Data: hours studied vs exam scores
hours = [1, 2, 3, 4, 5]
scores = [50, 55, 65, 70, 80]

plt.scatter(hours, scores, color='green', marker='o')
plt.title('Hours Studied vs Exam Scores')
plt.xlabel('Hours Studied')
plt.ylabel('Exam Score')
plt.grid(True)
plt.show()
OutputSuccess
Important Notes

Make sure x and y have the same length, or you will get an error.

You can add labels and a title to make the plot easier to understand.

Use plt.show() to display the plot window.

Summary

A scatter plot shows points for two sets of numbers.

Use plt.scatter(x, y) with your data lists.

You can customize colors and shapes of points.