Bird
Raised Fist0
Matplotlibdata~15 mins

Bubble charts concept in Matplotlib - Deep Dive

Choose your learning style10 modes available
Overview - Bubble charts concept
What is it?
A bubble chart is a type of scatter plot where each point is represented by a circle (bubble). The position of the bubble shows two values, like X and Y coordinates. The size of the bubble shows a third value, adding more information in the same chart. This helps us see relationships between three variables at once.
Why it matters
Bubble charts let us understand complex data with three variables visually, making patterns and differences easy to spot. Without bubble charts, we might miss how one variable changes with two others or struggle to show all data clearly in one picture. This can slow down decision-making or hide important insights.
Where it fits
Before learning bubble charts, you should know basic scatter plots and how to plot points on a graph. After mastering bubble charts, you can explore advanced visualization techniques like 3D plots or interactive charts to handle even more complex data.
Mental Model
Core Idea
A bubble chart shows three data values by using the position of a circle for two values and the circle's size for the third value.
Think of it like...
Imagine a map where cities are dots placed by their location (latitude and longitude), but the size of each dot shows how many people live there. The bigger the dot, the bigger the city.
  Y-axis
    ↑
    │       ● (big bubble)
    │   ● (medium bubble)
    │ ● (small bubble)
    └────────────────→ X-axis

Bubble size represents a third variable.
Build-Up - 6 Steps
1
FoundationUnderstanding scatter plots basics
🤔
Concept: Learn how scatter plots show relationships between two variables using points on a graph.
A scatter plot places dots on a graph where the X position shows one value and the Y position shows another. For example, plotting height vs weight for people shows if taller people weigh more.
Result
You get a graph with dots scattered showing how two variables relate.
Understanding scatter plots is essential because bubble charts build on this by adding a third variable.
2
FoundationIntroducing bubble size as data
🤔
Concept: Learn that the size of a circle can represent a number, adding a third dimension to a 2D plot.
Instead of just dots, we use circles with different sizes. For example, in a city map, the circle size can show population, while position shows location.
Result
Circles of different sizes appear on the graph, each size meaning something important.
Using size to show data lets us see more information in one picture without adding confusing extra axes.
3
IntermediateCreating bubble charts with matplotlib
🤔Before reading on: Do you think bubble size in matplotlib is controlled by the 'size' or 's' parameter? Commit to your answer.
Concept: Learn how to use matplotlib's scatter function to make bubble charts by setting the size parameter.
In matplotlib, you use plt.scatter(x, y, s=sizes) where 'sizes' is a list of numbers controlling bubble sizes. For example: import matplotlib.pyplot as plt x = [1, 2, 3] y = [4, 5, 6] sizes = [100, 300, 600] plt.scatter(x, y, s=sizes) plt.show()
Result
A plot appears with three bubbles at positions (1,4), (2,5), (3,6) with sizes 100, 300, and 600.
Knowing how to control bubble size in code lets you visually encode a third variable easily.
4
IntermediateAdding color for extra insight
🤔Before reading on: Can color in a bubble chart represent a fourth variable or just decoration? Commit to your answer.
Concept: Learn that color can add another layer of information, showing a fourth variable or categories.
You can add a 'c' parameter in plt.scatter to color bubbles by values. For example: colors = [10, 20, 30] plt.scatter(x, y, s=sizes, c=colors, cmap='viridis') plt.colorbar() plt.show()
Result
Bubbles appear with colors changing according to the 'colors' values, adding visual meaning.
Using color with size and position helps show even more data dimensions clearly.
5
AdvancedScaling bubble sizes correctly
🤔Before reading on: Should bubble sizes be proportional to raw data values or their square roots? Commit to your answer.
Concept: Learn that bubble area, not radius, should represent data to avoid misleading visuals.
If you set bubble size directly from data, big values look too large because size controls area. Instead, use the square root of data for size: import numpy as np sizes = np.sqrt(data_values) * scale_factor plt.scatter(x, y, s=sizes) plt.show()
Result
Bubbles sizes better reflect data differences without exaggeration.
Understanding size scaling prevents visual distortion and misinterpretation of data.
6
ExpertHandling overlapping bubbles and transparency
🤔Before reading on: Does adding transparency to bubbles help see overlapping data or make it harder? Commit to your answer.
Concept: Learn techniques to improve readability when bubbles overlap, like transparency and layering.
Use the 'alpha' parameter to make bubbles partly see-through: plt.scatter(x, y, s=sizes, alpha=0.5) plt.show() You can also sort data so bigger bubbles are drawn first or last to control visibility.
Result
Overlapping bubbles become easier to distinguish, revealing hidden data points.
Managing overlap with transparency and order improves chart clarity in dense data.
Under the Hood
Matplotlib's scatter function draws circles at given X and Y coordinates. The 's' parameter controls the area of each circle in points squared, not the radius. Internally, matplotlib converts these sizes to pixels on the screen. When colors are added, it maps data values to colors using a colormap. Transparency is handled by blending bubble colors with the background. The rendering order affects which bubbles appear on top.
Why designed this way?
Using area for size matches human perception better because we judge circle size by area, not radius. The separation of position, size, and color parameters allows flexible encoding of multiple data dimensions. Transparency and layering were added to solve the common problem of overlapping data points in dense plots.
Input data → [x, y, size, color] → matplotlib scatter function
  │
  ├─ Position: plots points at (x, y)
  ├─ Size: sets circle area proportional to 's'
  ├─ Color: maps values to colors via colormap
  └─ Alpha: sets transparency level

Rendering order → Draw bubbles on canvas → Final bubble chart
Myth Busters - 3 Common Misconceptions
Quick: Does the 's' parameter in matplotlib scatter control radius or area? Commit to radius or area.
Common Belief:The 's' parameter controls the radius of the bubbles directly.
Tap to reveal reality
Reality:The 's' parameter controls the area of the bubbles, so size values are squared units.
Why it matters:If you treat 's' as radius, bubble sizes will be visually misleading, exaggerating differences.
Quick: Can bubble charts show more than three variables effectively? Commit yes or no.
Common Belief:Bubble charts can clearly show many variables by adding size, color, shape, and labels all at once.
Tap to reveal reality
Reality:Adding too many variables makes bubble charts cluttered and hard to read; usually, 3-4 variables max work well.
Why it matters:Trying to show too much data in one bubble chart can confuse viewers and hide important patterns.
Quick: Does bigger bubble always mean bigger value? Commit yes or no.
Common Belief:Bigger bubbles always mean bigger data values directly.
Tap to reveal reality
Reality:If sizes are not scaled properly (e.g., using radius instead of area), bigger bubbles can misrepresent data magnitude.
Why it matters:Misinterpreting bubble size can lead to wrong conclusions about data relationships.
Expert Zone
1
Bubble size scaling often requires domain knowledge to choose appropriate scale factors for meaningful visual comparison.
2
Color maps can introduce bias; choosing perceptually uniform colormaps avoids misleading interpretations.
3
Plot layering order affects which bubbles are visible; sorting data before plotting can highlight important points.
When NOT to use
Bubble charts are not suitable when data points overlap heavily without clear separation or when precise numeric comparison is needed. Alternatives include heatmaps, 3D plots, or small multiples of simpler charts.
Production Patterns
Professionals use bubble charts in dashboards to show sales data (location, revenue, and customer count), in finance to plot risk vs return with portfolio size, and in healthcare to visualize patient data with multiple health indicators.
Connections
Scatter plots
Bubble charts build on scatter plots by adding size as a third variable.
Understanding scatter plots is essential because bubble charts extend them to show more data dimensions visually.
Heatmaps
Both visualize data density and relationships but heatmaps use color intensity on grids, while bubble charts use size and position.
Knowing heatmaps helps appreciate when bubble charts are better for showing individual data points versus aggregated data.
Cartography (Map visualization)
Bubble charts share the idea of using circle size to represent quantities on maps.
Recognizing this connection helps understand how visual variables like size communicate data across different fields.
Common Pitfalls
#1Using raw data values directly as bubble sizes without scaling.
Wrong approach:plt.scatter(x, y, s=data_values) plt.show()
Correct approach:plt.scatter(x, y, s=np.sqrt(data_values) * scale_factor) plt.show()
Root cause:Misunderstanding that 's' controls area, not radius, leading to exaggerated bubble sizes.
#2Not handling overlapping bubbles, causing important data to be hidden.
Wrong approach:plt.scatter(x, y, s=sizes) plt.show()
Correct approach:plt.scatter(x, y, s=sizes, alpha=0.5) plt.show()
Root cause:Ignoring visual clutter and overlap reduces chart readability.
#3Using a non-uniform colormap that misleads interpretation of color data.
Wrong approach:plt.scatter(x, y, s=sizes, c=colors, cmap='jet') plt.show()
Correct approach:plt.scatter(x, y, s=sizes, c=colors, cmap='viridis') plt.show()
Root cause:Choosing colormaps without considering perceptual uniformity causes misinterpretation.
Key Takeaways
Bubble charts extend scatter plots by using circle size to represent a third variable, making complex data easier to understand visually.
Proper scaling of bubble sizes is crucial to avoid misleading viewers about data magnitude.
Adding color and transparency can enrich bubble charts but must be used carefully to maintain clarity.
Understanding the internal mechanics of how size and color map to visual elements helps create accurate and effective charts.
Bubble charts are powerful but have limits; knowing when and how to use them ensures better data communication.

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