Scatter plots help us see how two things change together. They show if one thing goes up when the other goes up or down.
Why scatter plots show relationships in Matplotlib
Introduction
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
Matplotlib
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] plt.scatter(x, y) plt.show()
Matplotlib
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [10, 8, 6, 4, 2] plt.scatter(x, y) plt.show()
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()
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.
Practice
1. What is the main reason we use scatter plots in data science?
easy
Solution
Step 1: Understand the purpose of scatter plots
Scatter plots show points representing two variables on a graph to visualize their relationship.Step 2: Compare options with scatter plot use
Calculating averages, sorting, or listing unique values are not visual tasks done by scatter plots.Final Answer:
To see how two variables relate to each other -> Option BQuick Check:
Scatter plots show relationships = C [OK]
Hint: Scatter plots show points for two variables to find relationships [OK]
Common Mistakes:
- Confusing scatter plots with bar charts
- Thinking scatter plots calculate statistics
- Believing scatter plots sort data
2. Which of the following is the correct way to create a scatter plot using matplotlib?
easy
Solution
Step 1: Recall matplotlib scatter plot syntax
The correct function to create scatter plots in matplotlib is plt.scatter(x, y).Step 2: Check other options for correctness
plt.plot with 'scatter' is invalid, plt.bar creates bar charts, plt.scatterplot does not exist in matplotlib.Final Answer:
plt.scatter(x, y) -> Option DQuick Check:
Correct scatter plot function = D [OK]
Hint: Use plt.scatter(x, y) to plot scatter plots in matplotlib [OK]
Common Mistakes:
- Using plt.plot with wrong style
- Confusing bar charts with scatter plots
- Trying non-existent functions like plt.scatterplot
3. What will the following code show?
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] plt.scatter(x, y) plt.show()
medium
Solution
Step 1: Analyze the data points
x and y values increase proportionally: y is 2 times x, so points lie on a straight line.Step 2: Understand scatter plot output
Scatter plot will show points aligned diagonally upwards, not random or bars, and no error occurs without labels.Final Answer:
A scatter plot showing points forming a straight line going up -> Option AQuick Check:
Proportional points form line = B [OK]
Hint: Check if y changes proportionally with x for line pattern [OK]
Common Mistakes:
- Thinking scatter plots create bar charts
- Expecting error without labels
- Assuming points are random without checking data
4. The code below is intended to plot a scatter plot but shows an error. What is the problem?
import matplotlib.pyplot as plt x = [1, 2, 3] y = [4, 5] plt.scatter(x, y) plt.show()
medium
Solution
Step 1: Check the lengths of x and y
x has 3 values, y has 2 values; scatter plot needs equal length lists for x and y.Step 2: Verify other options
Labels are optional, integers are valid, plt.show() is present, so these are not errors.Final Answer:
x and y lists have different lengths -> Option CQuick Check:
Equal length x and y needed = A [OK]
Hint: Ensure x and y lists have same length before plotting [OK]
Common Mistakes:
- Thinking labels are mandatory
- Believing plt.scatter can't plot integers
- Forgetting to check list lengths
5. You want to show a negative relationship between two variables using a scatter plot. Which data and code will best show this?
hard
Solution
Step 1: Identify negative relationship in data
In x = [1, 2, 3, 4]; y = [10, 8, 6, 4]; plt.scatter(x, y), as x increases, y decreases, showing a negative relationship.Step 2: Check other options for relationship type
x = [1, 2, 3, 4]; y = [2, 4, 6, 8]; plt.scatter(x, y) shows positive relationship, C shows no change, D shows no clear pattern.Final Answer:
x = [1, 2, 3, 4]; y = [10, 8, 6, 4]; plt.scatter(x, y) -> Option AQuick Check:
y decreases as x increases = A [OK]
Hint: Negative relationship means y goes down as x goes up [OK]
Common Mistakes:
- Confusing positive and negative trends
- Choosing data with no clear pattern
- Ignoring constant y values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
