Bird
Raised Fist0
Matplotlibdata~20 mins

Marker size variation in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Marker Size Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of varying marker sizes in scatter plot
What will be the output of the following code snippet that uses matplotlib to plot points with different marker sizes?
Matplotlib
import matplotlib.pyplot as plt
sizes = [20, 50, 80, 200]
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.scatter(x, y, s=sizes)
plt.show()
AA scatter plot with 4 points, each point having marker sizes 20, 50, 80, and 200 respectively.
BA scatter plot with 4 points, but all markers are invisible because size is zero.
CA scatter plot with 4 points, all having the same marker size equal to the sum of sizes list.
DA scatter plot with 4 points, but marker sizes are ignored and default size is used.
Attempts:
2 left
💡 Hint
The 's' parameter in plt.scatter controls the marker size for each point individually if given a list.
❓ data_output
intermediate
1:30remaining
Number of points with marker size greater than 50
Given the following code, how many points have marker sizes greater than 50?
Matplotlib
import matplotlib.pyplot as plt
sizes = [10, 60, 45, 90, 30]
x = [1, 2, 3, 4, 5]
y = [5, 15, 10, 20, 25]
plt.scatter(x, y, s=sizes)
plt.show()
A3
B2
C1
D4
Attempts:
2 left
💡 Hint
Check the sizes list and count how many values are greater than 50.
🔧 Debug
advanced
2:00remaining
Identify the error in marker size assignment
What error will this code raise when trying to plot marker sizes?
Matplotlib
import matplotlib.pyplot as plt
sizes = '20, 40, 60'
x = [1, 2, 3]
y = [5, 10, 15]
plt.scatter(x, y, s=sizes)
plt.show()
ATypeError: float() argument must be a string or a number, not 'tuple'
BNo error, plot displays with marker sizes 20, 40, 60
CTypeError: 'str' object is not iterable
DValueError: s must be a scalar or sequence of scalars
Attempts:
2 left
💡 Hint
Check the type of 'sizes' and what plt.scatter expects for 's'.
❓ visualization
advanced
2:00remaining
Effect of marker size scaling on scatter plot
Which option correctly describes the visual difference when doubling the marker sizes in this scatter plot?
Matplotlib
import matplotlib.pyplot as plt
sizes = [30, 60, 90]
x = [1, 2, 3]
y = [10, 20, 30]
plt.scatter(x, y, s=[size * 2 for size in sizes])
plt.show()
AMarkers appear the same size as original because scaling is ignored.
BMarkers appear twice as large in diameter compared to original sizes.
CMarkers appear twice as large in area compared to original sizes.
DMarkers appear half the size of original because sizes are divided.
Attempts:
2 left
💡 Hint
Remember that 's' controls marker area, not diameter.
🚀 Application
expert
2:30remaining
Create a scatter plot with marker sizes proportional to y-values
Which code snippet correctly creates a scatter plot where marker sizes are proportional to the y-values multiplied by 10?
Aplt.scatter(x, y, s=[val * 10 for val in y])
Bplt.scatter(x, y, s=y * 10)
Cplt.scatter(x, y, s=[y * 10])
Dplt.scatter(x, y, s=list(map(lambda x: x*10, x)))
Attempts:
2 left
💡 Hint
You need to multiply each y-value by 10 to get marker sizes.

Practice

(1/5)
1. What does the s parameter control in the plt.scatter() function in matplotlib?
easy
A. The size of the markers in the scatter plot
B. The color of the markers
C. The shape of the markers
D. The transparency of the markers

Solution

  1. Step 1: Understand the role of plt.scatter()

    The plt.scatter() function creates scatter plots where each point can have different visual properties.
  2. Step 2: Identify the s parameter purpose

    The s parameter specifically sets the size of each marker in the scatter plot, either as a single value or a list of sizes.
  3. Final Answer:

    The size of the markers in the scatter plot -> Option A
  4. Quick Check:

    s controls marker size [OK]
Hint: Remember: 's' stands for size in scatter plots [OK]
Common Mistakes:
  • Confusing 's' with color or shape parameters
  • Thinking 's' controls transparency
  • Assuming 's' changes marker style
2. Which of the following is the correct way to set marker sizes to 50 for all points in a scatter plot using matplotlib?
easy
A. plt.scatter(x, y, size=50)
B. plt.scatter(x, y, s=50)
C. plt.scatter(x, y, marker_size=50)
D. plt.scatter(x, y, sizes=50)

Solution

  1. Step 1: Recall the parameter name for marker size

    The correct parameter to set marker size in plt.scatter() is s.
  2. Step 2: Check the syntax for setting size

    Using s=50 sets all markers to size 50. Other options use incorrect parameter names.
  3. Final Answer:

    plt.scatter(x, y, s=50) -> Option B
  4. Quick Check:

    Use s for size [OK]
Hint: Use s to set size, not 'size' or 'sizes' [OK]
Common Mistakes:
  • Using 'size' or 'sizes' instead of 's'
  • Trying to set marker size with 'marker_size'
  • Syntax errors from wrong parameter names
3. What will be the output marker sizes for the scatter plot from this code?
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
sizes = [20, 50, 80]
plt.scatter(x, y, s=sizes)
plt.show()
medium
A. All markers will have size 20
B. Code will raise an error due to wrong size input
C. Markers will have default size because list is invalid
D. Markers will have sizes 20, 50, and 80 respectively

Solution

  1. Step 1: Understand s with list input

    The s parameter accepts a list to assign different sizes to each marker.
  2. Step 2: Match sizes to points

    Since sizes = [20, 50, 80], the first point gets size 20, second 50, third 80.
  3. Final Answer:

    Markers will have sizes 20, 50, and 80 respectively -> Option D
  4. Quick Check:

    List in s sets sizes per point [OK]
Hint: List in s sets sizes point-wise [OK]
Common Mistakes:
  • Assuming all markers get the first size
  • Thinking list input causes error
  • Confusing s with color list
4. Identify the error in this code snippet that tries to vary marker sizes:
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
sizes = [30, 60]
plt.scatter(x, y, s=sizes)
plt.show()
medium
A. The plt.scatter function is misspelled
B. The s parameter cannot accept lists
C. The sizes list length does not match x and y length
D. The variable sizes is not defined

Solution

  1. Step 1: Check lengths of data and sizes

    The x and y lists have 3 points, but sizes list has only 2 values.
  2. Step 2: Understand size list requirement

    The s list must match the number of points; otherwise, matplotlib raises a ValueError.
  3. Final Answer:

    The sizes list length does not match x and y length -> Option C
  4. Quick Check:

    Sizes list length must equal data points [OK]
Hint: Match sizes list length to data points [OK]
Common Mistakes:
  • Ignoring size list length mismatch
  • Thinking s can't be a list
  • Assuming matplotlib auto-fills missing sizes
5. You want to create a scatter plot where marker sizes represent the population of cities. Given:
cities = ['A', 'B', 'C']
population = [1000, 5000, 3000]
x = [1, 2, 3]
y = [4, 5, 6]

Which code correctly scales the population to marker sizes and plots them?
hard
A. plt.scatter(x, y, s=[p*10 for p in population])
B. plt.scatter(x, y, s=[p/10 for p in population])
C. plt.scatter(x, y, s=population)
D. plt.scatter(x, y, s=[p**2 for p in population])

Solution

  1. Step 1: Understand marker size scaling

    Population values are large; directly using them as sizes will create huge markers.
  2. Step 2: Choose appropriate scaling

    Multiplying population by 10 scales sizes visibly but keeps them reasonable. Dividing by 10 makes markers too small; squaring makes them too large.
  3. Final Answer:

    plt.scatter(x, y, s=[p*10 for p in population]) -> Option A
  4. Quick Check:

    Scale sizes for visibility using multiplication [OK]
Hint: Scale data to reasonable marker sizes by multiplying [OK]
Common Mistakes:
  • Using raw large values causing huge markers
  • Dividing sizes too much making markers tiny
  • Using squared sizes causing disproportionate markers
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