Bird
Raised Fist0
Matplotlibdata~20 mins

Bubble charts concept in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bubble Chart Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Bubble Chart Size Scaling
What will be the size of the bubbles in the plot generated by the following code snippet?
Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
sizes = [10, 20, 30]
plt.scatter(x, y, s=sizes)
plt.show()
print(sizes)
A[100, 400, 900]
B[10, 20, 30]
C[1, 2, 3]
D[10, 20, 30, 40]
Attempts:
2 left
💡 Hint
The 's' parameter in plt.scatter controls the area of each bubble, not the radius.
❓ data_output
intermediate
2:00remaining
Number of Bubbles Displayed
Given the following data and code, how many bubbles will appear in the bubble chart?
Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 30, 40]
sizes = [15, 25, 35]
plt.scatter(x, y, s=sizes)
plt.show()
ARaises ValueError
B4
C3
D0
Attempts:
2 left
💡 Hint
The lengths of x, y, and sizes must match for plt.scatter.
❓ visualization
advanced
2:30remaining
Interpreting Bubble Chart Colors and Sizes
Consider this code that creates a bubble chart with color and size representing different data aspects. What does the color represent in the resulting plot?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5, 15, 25, 35, 45])
y = np.array([10, 20, 30, 40, 50])
sizes = np.array([100, 200, 300, 400, 500])
colors = np.array([1, 2, 3, 4, 5])
plt.scatter(x, y, s=sizes, c=colors, cmap='viridis')
plt.colorbar()
plt.show()
AThe colors represent the sizes of the bubbles.
BThe colors represent the x-axis values.
CThe colors are randomly assigned and do not represent any data.
DThe colors represent the values in the 'colors' array, mapped to the 'viridis' color scale.
Attempts:
2 left
💡 Hint
The 'c' parameter controls color mapping in plt.scatter.
🔧 Debug
advanced
2:00remaining
Identify the Error in Bubble Chart Code
What error will this code produce when run?
Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5]
sizes = [10, 20, 30]
plt.scatter(x, y, s=sizes)
plt.show()
AValueError: x and y must be the same size
BTypeError: sizes must be a scalar or array-like
CIndexError: list index out of range
DNo error, plot displays correctly
Attempts:
2 left
💡 Hint
Check if x and y have the same number of elements.
🚀 Application
expert
3:00remaining
Choosing Bubble Sizes for Visual Clarity
You have data points with values ranging from 1 to 1000. You want to create a bubble chart where bubble sizes reflect these values but remain visually clear and not too large. Which approach is best to set the bubble sizes?
AUse the raw values directly as sizes in plt.scatter.
BUse the logarithm (base 10) of the values as sizes without scaling.
CApply a square root transformation to the values before passing as sizes.
DNormalize values between 0 and 1 and use them directly as sizes.
Attempts:
2 left
💡 Hint
Bubble area grows with the size parameter, so large raw values can cause huge bubbles.

Practice

(1/5)
1. What does the size of bubbles represent in a bubble chart created with matplotlib?
easy
A. The color of the bubbles
B. A third data value representing magnitude or quantity
C. The x-axis position
D. The y-axis position

Solution

  1. Step 1: Understand bubble chart data dimensions

    A bubble chart shows data points with x and y coordinates plus a size dimension.
  2. Step 2: Identify what bubble size represents

    The size of each bubble encodes a third data value, like quantity or magnitude.
  3. Final Answer:

    A third data value representing magnitude or quantity -> Option B
  4. Quick Check:

    Bubble size = third data value [OK]
Hint: Bubble size shows extra data beyond x and y [OK]
Common Mistakes:
  • Confusing size with color
  • Thinking size is x or y position
  • Ignoring the third data dimension
2. Which matplotlib function parameter controls the bubble sizes in a bubble chart?
easy
A. size
B. bubble_size
C. radius
D. s

Solution

  1. Step 1: Recall plt.scatter parameters

    The plt.scatter function uses s to set bubble sizes.
  2. Step 2: Confirm correct parameter name

    Other options like size or radius are not valid parameters for bubble size in plt.scatter.
  3. Final Answer:

    s -> Option D
  4. Quick Check:

    plt.scatter(x, y, s=...) sets bubble size [OK]
Hint: Use 's' parameter for bubble sizes in plt.scatter [OK]
Common Mistakes:
  • Using 'size' instead of 's'
  • Trying 'radius' which is invalid
  • Confusing with color parameter
3. What will be the output of this code snippet?
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
sizes = [100, 200, 300]
plt.scatter(x, y, s=sizes)
plt.show()
medium
A. A syntax error due to incorrect parameter
B. A scatter plot with all bubbles the same size
C. A scatter plot with three bubbles sized 100, 200, and 300 respectively
D. A scatter plot with bubbles sized 1, 2, and 3

Solution

  1. Step 1: Analyze the code parameters

    The code uses plt.scatter with x, y coordinates and s=sizes where sizes are [100, 200, 300].
  2. Step 2: Understand bubble size mapping

    Each bubble size corresponds to the values in the sizes list, so bubbles will have sizes 100, 200, and 300.
  3. Final Answer:

    A scatter plot with three bubbles sized 100, 200, and 300 respectively -> Option C
  4. Quick Check:

    Bubble sizes match 's' list values [OK]
Hint: Bubble sizes come from the 's' list values [OK]
Common Mistakes:
  • Assuming all bubbles have same size
  • Confusing sizes with x or y values
  • Expecting syntax error incorrectly
4. Identify the error in this bubble chart code:
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
sizes = [100, 200]
plt.scatter(x, y, s=sizes)
plt.show()
medium
A. The sizes list length does not match x and y lengths
B. The parameter 's' is misspelled
C. x and y must be strings, not lists
D. plt.scatter cannot plot more than two points

Solution

  1. Step 1: Check lengths of x, y, and sizes

    x and y have 3 elements each, but sizes has only 2 elements.
  2. Step 2: Understand size array length requirement

    The sizes array must match the number of points to assign size to each bubble.
  3. Final Answer:

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

    Sizes length must equal x and y length [OK]
Hint: Sizes array length must match x and y arrays [OK]
Common Mistakes:
  • Ignoring size list length mismatch
  • Thinking 's' parameter is misspelled
  • Believing x and y must be strings
5. You want to create a bubble chart where bubble sizes represent sales volume, but sales data has zeros and very large values. Which approach is best to make the chart readable?
hard
A. Normalize sales data and apply a size scaling factor
B. Use raw sales values directly as sizes
C. Set all bubble sizes to a fixed constant
D. Use sales data as x-axis values instead

Solution

  1. Step 1: Understand impact of raw sizes

    Using raw sales values with zeros and large numbers causes bubbles too small or too large, making chart unreadable.
  2. Step 2: Normalize and scale sizes

    Normalizing sales data (e.g., min-max scaling) and applying a size factor keeps bubbles proportional and visible.
  3. Step 3: Evaluate other options

    Fixed sizes lose data meaning; using sales as x-axis changes chart type and loses size info.
  4. Final Answer:

    Normalize sales data and apply a size scaling factor -> Option A
  5. Quick Check:

    Normalize and scale sizes for readability [OK]
Hint: Normalize and scale sizes to handle zeros and extremes [OK]
Common Mistakes:
  • Using raw sizes causing unreadable bubbles
  • Fixing all sizes losing data info
  • Misusing sales data as axis instead of size
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