Scatter plots help us see how two things change together. They show if one thing goes up when the other goes up or down.
0
0
Why scatter plots show relationships in Matplotlib
Introduction
To check if study time and test scores are connected.
To see if temperature affects ice cream sales.
To find patterns between height and weight.
To explore if advertising budget relates to product sales.
Syntax
Matplotlib
import matplotlib.pyplot as plt plt.scatter(x_values, y_values) plt.xlabel('X label') plt.ylabel('Y label') plt.title('Title of the plot') plt.show()
x_values and y_values are lists or arrays of numbers.
Each point on the plot shows one pair of x and y values.
Examples
This shows a clear upward trend: as x increases, y also increases.
Matplotlib
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] plt.scatter(x, y) plt.show()
This shows a downward trend: as x increases, y decreases.
Matplotlib
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [10, 8, 6, 4, 2] plt.scatter(x, y) plt.show()
This shows no relationship: y stays the same no matter what x is.
Matplotlib
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [5, 5, 5, 5, 5] plt.scatter(x, y) plt.show()
Sample Program
This plot shows that as hours studied increase, test scores tend to increase too. It helps us see the positive relationship clearly.
Matplotlib
import matplotlib.pyplot as plt # Data: hours studied vs test scores hours_studied = [1, 2, 3, 4, 5, 6, 7, 8] scores = [50, 55, 65, 70, 75, 80, 85, 90] plt.scatter(hours_studied, scores) plt.xlabel('Hours Studied') plt.ylabel('Test Score') plt.title('Relationship between Study Time and Test Scores') plt.grid(True) plt.show()
OutputSuccess
Important Notes
Scatter plots are great for spotting trends, clusters, or outliers in data.
Adding labels and a title helps others understand what the plot shows.
Grid lines can make it easier to read exact values on the plot.
Summary
Scatter plots show how two sets of numbers relate by plotting points.
They help us see if one thing changes when another changes.
Use them to find patterns or check if two things are connected.