📖 Scenario: You work as a data analyst for a small company. Your manager wants to see a visual comparison of sales data for different products. You will create a bubble chart to show the sales volume, profit, and number of customers for each product.
🎯 Goal: Build a bubble chart using matplotlib that shows product sales data. The x-axis will represent sales volume, the y-axis will represent profit, and the size of each bubble will represent the number of customers.
📋 What You'll Learn
Create a dictionary called sales_data with product names as keys and tuples of (sales volume, profit, customers) as values.
Create a variable called scale_factor to adjust bubble sizes.
Use a for loop with variables product and values to iterate over sales_data.items().
Plot bubbles using plt.scatter with correct x, y, and size values.
Add labels for x-axis, y-axis, and a title.
Print the plot using plt.show().
💡 Why This Matters
🌍 Real World
Bubble charts help businesses visualize relationships between three variables at once, such as sales, profit, and customer count.
💼 Career
Data analysts and scientists use bubble charts to present complex data clearly to managers and stakeholders.
Progress0 / 4 steps
1
Set up the sales data dictionary
Create a dictionary called sales_data with these exact entries: 'Product A': (100, 30, 40), 'Product B': (150, 45, 60), 'Product C': (80, 20, 30), 'Product D': (200, 70, 90).
Matplotlib
Hint
Use curly braces {} to create the dictionary. Each key is a product name string, and each value is a tuple with three numbers.
2
Create a scale factor for bubble sizes
Create a variable called scale_factor and set it to 10. This will help adjust the size of the bubbles in the chart.
Matplotlib
Hint
Just write scale_factor = 10 on a new line.
3
Plot the bubble chart using a for loop
Import matplotlib.pyplot as plt. Use a for loop with variables product and values to iterate over sales_data.items(). Inside the loop, use plt.scatter to plot bubbles where x is sales volume, y is profit, and size is customers multiplied by scale_factor. Add labels for x-axis as 'Sales Volume', y-axis as 'Profit', and a title 'Sales Bubble Chart'.
Matplotlib
Hint
Remember to unpack values into sales_volume, profit, and customers. Use plt.scatter(x, y, s=size) to plot bubbles. Use alpha=0.5 for transparency and label=product to label each bubble.
4
Display the bubble chart
Add a legend to the plot using plt.legend(). Then print the plot using plt.show().
Matplotlib
Hint
Use plt.legend() to show the labels for each bubble. Then call plt.show() to display the chart.
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
Step 1: Understand bubble chart data dimensions
A bubble chart shows data points with x and y coordinates plus a size dimension.
Step 2: Identify what bubble size represents
The size of each bubble encodes a third data value, like quantity or magnitude.
Final Answer:
A third data value representing magnitude or quantity -> Option B
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
Step 1: Recall plt.scatter parameters
The plt.scatter function uses s to set bubble sizes.
Step 2: Confirm correct parameter name
Other options like size or radius are not valid parameters for bubble size in plt.scatter.
Final Answer:
s -> Option D
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
Step 1: Analyze the code parameters
The code uses plt.scatter with x, y coordinates and s=sizes where sizes are [100, 200, 300].
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.
Final Answer:
A scatter plot with three bubbles sized 100, 200, and 300 respectively -> Option C
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
Step 1: Check lengths of x, y, and sizes
x and y have 3 elements each, but sizes has only 2 elements.
Step 2: Understand size array length requirement
The sizes array must match the number of points to assign size to each bubble.
Final Answer:
The sizes list length does not match x and y lengths -> Option A
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
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.
Step 2: Normalize and scale sizes
Normalizing sales data (e.g., min-max scaling) and applying a size factor keeps bubbles proportional and visible.
Step 3: Evaluate other options
Fixed sizes lose data meaning; using sales as x-axis changes chart type and loses size info.
Final Answer:
Normalize sales data and apply a size scaling factor -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.