📖 Scenario: You are a scientist who collected data about the heights and weights of a group of people. You want to visualize this data to see if there is any pattern between height and weight.
🎯 Goal: Create a scatter plot in MATLAB that shows the relationship between height and weight.
📋 What You'll Learn
Create two vectors called height and weight with given data
Create a variable called markerSize to control the size of the scatter plot points
Use the scatter function with height, weight, and markerSize
Add labels to the x-axis and y-axis
Display the scatter plot
💡 Why This Matters
🌍 Real World
Scientists and data analysts often use scatter plots to see if two things are related, like height and weight.
💼 Career
Knowing how to create scatter plots is useful in jobs involving data visualization, research, and reporting.
Progress0 / 4 steps
1
Create the data vectors
Create a vector called height with these exact values: [150, 160, 170, 180, 190] and a vector called weight with these exact values: [50, 60, 70, 80, 90].
MATLAB
Hint
Use square brackets [] to create vectors in MATLAB.
2
Set the marker size
Create a variable called markerSize and set it to 100 to control the size of the scatter plot points.
MATLAB
Hint
Just assign the number 100 to the variable markerSize.
3
Create the scatter plot
Use the scatter function with height, weight, and markerSize to create the scatter plot.
MATLAB
Hint
Use scatter(height, weight, markerSize) to plot the points.
4
Add labels and display the plot
Add the x-axis label 'Height (cm)' using xlabel and the y-axis label 'Weight (kg)' using ylabel. Then display the plot.
MATLAB
Hint
Use xlabel('Height (cm)') and ylabel('Weight (kg)') to add labels.
Practice
(1/5)
1. What does a scatter plot in MATLAB primarily show?
easy
A. A bar chart of grouped data
B. A continuous line connecting data points
C. Points representing pairs of values on an X-Y graph
D. A pie chart of category percentages
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 C
Quick 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
A. plot.scatter(x, y)
B. scatter(x, y)
C. scatterplot(x, y)
D. scatter x, y
Solution
Step 1: Recall MATLAB scatter syntax
The correct function to create scatter plots is scatter(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 B
Quick 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
A. A scatter plot with red filled circles sized 100 at points (1,2), (2,4), ..., (5,10)
B. A line plot connecting points from (1,2) to (5,10)
C. An error because 'filled' is not a valid option
D. A scatter plot with default size and blue color points
Solution
Step 1: Understand scatter parameters
The code uses scatter(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 A
Quick 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
A. Vectors x and y have different lengths causing an error
B. scatter requires a third argument for size
C. scatter cannot plot numeric vectors
D. No error; code runs fine
Solution
Step 1: Check vector lengths
Vector x has length 4, but y has length 3, which is invalid for scatter.
Step 2: Understand scatter input requirements
Scatter requires x and y to be the same length to plot pairs of points.
Final Answer:
Vectors x and y have different lengths causing an error -> Option A
Quick Check:
Length mismatch in x,y vectors = error [OK]
Hint: x and y must be same length for scatter [OK]