0
0
Matplotlibdata~5 mins

Bubble charts concept in Matplotlib

Choose your learning style9 modes available
Introduction

Bubble charts help you see relationships between three sets of numbers in one picture. They show points with different sizes to add more information.

Comparing sales, profit, and number of customers for different stores.
Showing countries' population, GDP, and area on one chart.
Visualizing students' test scores, study hours, and attendance.
Analyzing product price, rating, and number of reviews.
Syntax
Matplotlib
plt.scatter(x, y, s=sizes, alpha=opacity)
plt.xlabel('X label')
plt.ylabel('Y label')
plt.title('Chart title')
plt.show()

x and y are lists or arrays of coordinates.

s controls the size of each bubble (can be a list).

Examples
Simple bubble chart with three points of different sizes.
Matplotlib
plt.scatter([1, 2, 3], [4, 5, 6], s=[20, 50, 80])
plt.show()
Adding transparency and labels for clarity.
Matplotlib
plt.scatter(x, y, s=sizes, alpha=0.5)
plt.xlabel('Age')
plt.ylabel('Income')
plt.title('Age vs Income with Bubble Size')
plt.show()
Sample Program

This code shows how test scores relate to hours studied. The bubble size shows how many practice tests were done.

Matplotlib
import matplotlib.pyplot as plt

# Data: hours studied, test score, and number of practice tests
hours = [1, 2, 3, 4, 5]
scores = [55, 60, 65, 70, 75]
practice_tests = [5, 10, 15, 20, 25]

# Bubble sizes scaled for visibility
sizes = [p * 10 for p in practice_tests]

plt.scatter(hours, scores, s=sizes, alpha=0.6, color='blue')
plt.xlabel('Hours Studied')
plt.ylabel('Test Score')
plt.title('Test Scores vs Hours Studied with Practice Tests as Bubble Size')
plt.show()
OutputSuccess
Important Notes

Bubble size s should be scaled to avoid too small or too large bubbles.

Use alpha to make bubbles partly see-through if they overlap.

Label axes and add a title to make the chart easy to understand.

Summary

Bubble charts show three data values: x, y, and size.

They help compare data points with an extra dimension using bubble size.

Use labels and transparency for better readability.