Bird
Raised Fist0
Matplotlibdata~20 mins

Basic scatter plot with plt.scatter in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Scatter Plot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple scatter plot code
What will be the output of this code snippet that uses plt.scatter to plot points?
Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
plt.scatter(x, y)
plt.show()
AAn error because plt.scatter requires color argument.
BA line plot connecting points (1,4), (2,5), and (3,6).
CA scatter plot with three points at coordinates (1,4), (2,5), and (3,6) displayed.
DA bar chart with bars at x=1,2,3 and heights 4,5,6.
Attempts:
2 left
💡 Hint
Remember, plt.scatter plots points, not lines or bars.
❓ data_output
intermediate
1:30remaining
Number of points plotted by plt.scatter
How many points will be plotted by this code?
Matplotlib
import matplotlib.pyplot as plt
x = [10, 20, 30, 40]
y = [1, 2, 3, 4]
plt.scatter(x, y)
plt.show()
A4
B3
C5
D0
Attempts:
2 left
💡 Hint
Count the number of x or y values given.
🔧 Debug
advanced
2:00remaining
Identify the error in scatter plot code
What error will this code produce?
Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5]
plt.scatter(x, y)
plt.show()
AValueError: x and y must be the same size
BTypeError: plt.scatter() missing required positional argument
CNo error, plot shows 2 points
DIndexError: list index out of range
Attempts:
2 left
💡 Hint
Check if x and y lists have the same length.
❓ visualization
advanced
2:00remaining
Effect of marker size in plt.scatter
What will be the visual difference when running this code compared to default marker size?
Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
plt.scatter(x, y, s=200)
plt.show()
APoints will appear smaller than default size.
BCode will raise an error due to invalid size argument.
CPoints will be connected by lines.
DPoints will appear larger than default size.
Attempts:
2 left
💡 Hint
The 's' parameter controls marker size in plt.scatter.
🚀 Application
expert
3:00remaining
Plotting two groups with different colors using plt.scatter
Which code correctly plots two groups of points in different colors on the same scatter plot?
A
import matplotlib.pyplot as plt
x1, y1 = [1,2], [3,4]
x2, y2 = [5,6], [7,8]
plt.scatter(x1, y1)
plt.scatter(x2, y2)
plt.show()
B
import matplotlib.pyplot as plt
x1, y1 = [1,2], [3,4]
x2, y2 = [5,6], [7,8]
plt.scatter(x1, y1, color='red')
plt.scatter(x2, y2, color='blue')
plt.show()
C
import matplotlib.pyplot as plt
x = [1,2,5,6]
y = [3,4,7,8]
plt.scatter(x, y, color=['red', 'red', 'blue', 'blue'])
plt.show()
D
import matplotlib.pyplot as plt
x = [1,2,5,6]
y = [3,4,7,8]
plt.scatter(x, y, c='red blue')
plt.show()
Attempts:
2 left
💡 Hint
Use separate plt.scatter calls with color argument for each group.

Practice

(1/5)
1. What does the plt.scatter(x, y) function do in matplotlib?
easy
A. It creates a line plot connecting x and y points.
B. It creates a scatter plot showing points for x and y data.
C. It creates a bar chart for x and y values.
D. It creates a histogram of x values only.

Solution

  1. Step 1: Understand the purpose of plt.scatter

    The function plt.scatter is designed to plot individual points based on two lists or arrays, x and y, showing their relationship.
  2. Step 2: Compare with other plot types

    Unlike line plots or bar charts, scatter plots show points without connecting lines or bars.
  3. Final Answer:

    It creates a scatter plot showing points for x and y data. -> Option B
  4. Quick Check:

    Scatter plot = points for x and y [OK]
Hint: Scatter plots show points, not lines or bars [OK]
Common Mistakes:
  • Confusing scatter plot with line plot
  • Thinking it creates bars or histograms
  • Assuming it only plots x values
2. Which of the following is the correct syntax to create a scatter plot with x and y lists (x horizontal, y vertical) using matplotlib?
easy
A. plt.scatter(x, y)
B. plt.scatter(y, x)
C. plt.plot.scatter(x, y)
D. scatter.plot(x, y)

Solution

  1. Step 1: Recall the correct function call

    The correct function to create a scatter plot in matplotlib is plt.scatter(x, y), where x and y are data lists.
  2. Step 2: Identify incorrect syntax

    Options with reversed arguments or wrong method chaining like plt.plot.scatter or scatter.plot are invalid.
  3. Final Answer:

    plt.scatter(x, y) -> Option A
  4. Quick Check:

    Correct syntax = plt.scatter(x, y) [OK]
Hint: Use plt.scatter(x, y) exactly for scatter plots [OK]
Common Mistakes:
  • Swapping x and y arguments
  • Using plt.plot.scatter instead of plt.scatter
  • Calling scatter on wrong object
3. What will be the output of this code?
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
plt.scatter(x, y)
plt.show()
medium
A. An error because plt.show() is missing.
B. A line plot connecting points (1,4), (2,5), and (3,6).
C. A scatter plot with points at (1,4), (2,5), and (3,6).
D. A bar chart with bars at x positions 1, 2, 3.

Solution

  1. Step 1: Understand plt.scatter with given data

    The code plots points at coordinates from lists x and y: (1,4), (2,5), and (3,6).
  2. Step 2: Confirm plt.show() displays the plot

    The plt.show() command displays the scatter plot window with these points.
  3. Final Answer:

    A scatter plot with points at (1,4), (2,5), and (3,6). -> Option C
  4. Quick Check:

    Scatter plot points = (1,4), (2,5), (3,6) [OK]
Hint: Scatter plots show points at (x[i], y[i]) pairs [OK]
Common Mistakes:
  • Thinking plt.scatter creates line plots
  • Assuming plt.show() is optional and causes error
  • Confusing scatter with bar chart
4. Identify the error in this code snippet:
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5]
plt.scatter(x, y)
plt.show()
medium
A. plt.scatter requires numpy arrays, not lists.
B. The code is correct and runs without error.
C. plt.show() is missing to display the plot.
D. x and y lists have different lengths causing an error.

Solution

  1. Step 1: Check lengths of x and y lists

    The x list has 3 elements, but y has only 2 elements. This mismatch causes plt.scatter to raise a ValueError.
  2. Step 2: Understand plt.scatter input requirements

    Both x and y must have the same number of points to plot pairs correctly.
  3. Final Answer:

    x and y lists have different lengths causing an error. -> Option D
  4. Quick Check:

    Equal length x and y needed [OK]
Hint: x and y must be same length for scatter plot [OK]
Common Mistakes:
  • Ignoring length mismatch of x and y
  • Thinking plt.scatter needs numpy arrays only
  • Assuming plt.show() missing causes error
5. You want to plot points with x = [1, 2, 3, 4] and y = [10, 20, 30, 40], but color points red if y > 25 and blue otherwise. Which code correctly does this?
hard
A. colors = ['red' if val > 25 else 'blue' for val in y] plt.scatter(x, y, c=colors) plt.show()
B. plt.scatter(x, y, color=['red' if y > 25 else 'blue']) plt.show()
C. plt.scatter(x, y, c='red' if y > 25 else 'blue') plt.show()
D. colors = ['red' if x > 25 else 'blue' for x in x] plt.scatter(x, y, c=colors) plt.show()

Solution

  1. Step 1: Create color list based on y values

    We use a list comprehension to assign 'red' if y value is greater than 25, else 'blue'. This creates a color list matching each point.
  2. Step 2: Pass color list to plt.scatter with c parameter

    Using c=colors applies the color list to points correctly. Then plt.show() displays the plot.
  3. Final Answer:

    colors = ['red' if val > 25 else 'blue' for val in y] plt.scatter(x, y, c=colors) plt.show() -> Option A
  4. Quick Check:

    Use list comprehension for colors, pass as c=colors [OK]
Hint: Use list comprehension for colors, pass with c=colors [OK]
Common Mistakes:
  • Using 'color' instead of 'c' parameter
  • Passing single color string instead of list
  • Checking condition on x instead of y
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard