Scatter plots help you see how two sets of numbers relate to each other. They show points on a graph so you can spot patterns or trends.
Scatter plots in MATLAB
Introduction
Syntax
MATLAB
scatter(x, y)
scatter(x, y, size)
scatter(x, y, size, color)
scatter(x, y, size, color, 'filled')x and y are vectors of the same length representing points.
You can change the size and color of points to add more information.
Examples
MATLAB
x = [1 2 3 4 5]; y = [2 4 1 3 5]; scatter(x, y)
MATLAB
x = rand(1,10); y = rand(1,10); scatter(x, y, 100)
MATLAB
x = 1:5; y = [5 3 6 2 7]; colors = [10 20 30 40 50]; scatter(x, y, 100, colors, 'filled')
Sample Program
This program plots prime numbers (y) against their position (x). Points are red and filled for clear visibility.
MATLAB
x = [1 2 3 4 5 6 7 8 9 10]; y = [2 3 5 7 11 13 17 19 23 29]; scatter(x, y, 100, 'r', 'filled') title('Scatter plot of x vs y') xlabel('x values') ylabel('y values')
Important Notes
Use hold on to add multiple scatter plots on the same figure.
Use colorbar to show what colors mean if you use color to represent data.
Always label your axes and add a title for clarity.
Summary
Scatter plots show points to explore relationships between two variables.
You can customize point size and color to add more meaning.
Label your plot to make it easy to understand.
Practice
1. What does a scatter plot in MATLAB primarily show?
easy
Solution
Step 1: Understand scatter plot purpose
A scatter plot shows individual points for pairs of values, usually X and Y coordinates.Step 2: Compare options to definition
Only Points representing pairs of values on an X-Y graph describes points on an X-Y graph, which matches scatter plot behavior.Final Answer:
Points representing pairs of values on an X-Y graph -> Option CQuick Check:
Scatter plot = points on X-Y graph [OK]
Hint: Scatter plots show points, not lines or bars [OK]
Common Mistakes:
- Confusing scatter plot with line plot
- Thinking scatter plots show bars or pie slices
- Assuming scatter plots connect points
2. Which of the following is the correct MATLAB syntax to create a scatter plot of vectors
x and y?easy
Solution
Step 1: Recall MATLAB scatter syntax
The correct function to create scatter plots isscatter(x, y).Step 2: Check other options for syntax errors
Options A, C, and D are not valid MATLAB commands for scatter plots.Final Answer:
scatter(x, y) -> Option BQuick Check:
scatter function syntax = scatter(x, y) [OK]
Hint: Use scatter() function with parentheses and commas [OK]
Common Mistakes:
- Using dot notation like plot.scatter
- Typing scatterplot instead of scatter
- Omitting parentheses or commas
3. What will the following MATLAB code display?
x = 1:5;
y = [2 4 6 8 10];
scatter(x, y, 100, 'r', 'filled')
medium
Solution
Step 1: Understand scatter parameters
The code usesscatter(x, y, 100, 'r', 'filled')which sets point size to 100, color red ('r'), and fills the markers.Step 2: Interpret the plot output
The plot shows red filled circles at points (1,2), (2,4), (3,6), (4,8), and (5,10).Final Answer:
A scatter plot with red filled circles sized 100 at points (1,2), (2,4), ..., (5,10) -> Option AQuick Check:
scatter(x,y,100,'r','filled') = red filled circles [OK]
Hint: Size, color, and fill control marker appearance [OK]
Common Mistakes:
- Thinking 'filled' causes error
- Confusing scatter with line plot
- Ignoring size and color parameters
4. Identify the error in this MATLAB code for a scatter plot:
x = 1:4;
y = [5 6 7];
scatter(x, y)
medium
Solution
Step 1: Check vector lengths
Vectorxhas length 4, butyhas length 3, which is invalid for scatter.Step 2: Understand scatter input requirements
Scatter requiresxandyto be the same length to plot pairs of points.Final Answer:
Vectors x and y have different lengths causing an error -> Option AQuick Check:
Length mismatch in x,y vectors = error [OK]
Hint: x and y must be same length for scatter [OK]
Common Mistakes:
- Assuming scatter auto-fixes length mismatch
- Thinking size argument is mandatory
- Believing scatter can't plot numeric data
5. You have two groups of data points:
How can you plot both groups on the same scatter plot with different colors using filled markers and add a legend?
x1 = [1 2 3]; y1 = [4 5 6];
x2 = [1 2 3]; y2 = [7 8 9];
How can you plot both groups on the same scatter plot with different colors using filled markers and add a legend?
hard
Solution
Step 1: Plot first group with filled blue markers
Usescatter(x1, y1, 'b', 'filled')to plot blue filled points.Step 2: Use
To overlay plots,hold onbefore plotting second grouphold onkeeps the first plot visible before adding the second.Step 3: Plot second group with filled red markers and add legend
Plot second group withscatter(x2, y2, 'r', 'filled')and add legend for clarity.Final Answer:
scatter(x1, y1, 'b', 'filled'); hold on; scatter(x2, y2, 'r', 'filled'); legend('Group 1', 'Group 2') -> Option DQuick Check:
Use hold on to overlay scatter plots with legend [OK]
Hint: Use hold on to combine scatter plots with different colors [OK]
Common Mistakes:
- Forgetting hold on causes second plot to overwrite first
- Using plot instead of scatter for points
- Omitting legend or using incorrect syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
