Bubble Chart in Plotly
Last Updated: 08 Nov 2025
Bubble charts are enhanced scatter plots where marker size = third variable — perfect for showing 3D relationships like GDP vs Life Expectancy vs Population, Sales vs Profit vs Region, or Risk vs Return vs Investment.
Hinglish Tip: Bubble = "teen cheez ek saath dikhao" — x, y, aur size! Bada bubble = zyada impact.
Basic Bubble Chart
import plotly.express as px
# Sample data
df = px.data.gapminder().query("year == 2007")
fig = px.scatter(
df,
x="gdpPercap",
y="lifeExp",
size="pop",
color="continent",
hover_name="country",
title="GDP vs Life Expectancy (Bubble = Population)"
)
fig.show()
Key Parameters in px.scatter() for Bubble
| Parameter | Values / Examples | Purpose |
|---|---|---|
| size | Column name | Bubble size (larger = bigger value) |
| size_max | 60 | Max bubble size (pixels) |
| color | Column | Color by category |
| hover_name | Column | Bold label on hover |
| hover_data | ['pop'] | Extra info on hover |
| log_x, log_y | True | Log scale (for skewed data) |
| labels | 'gdpPercap': 'GDP per Capita' | Rename axes |
| title | String | Chart title |
| opacity | 0.7 | Transparency (avoid overlap) |
Full Example: Company Performance (Revenue, Profit, Employees)
import plotly.express as px
import pandas as pd
# Sample business data
df = pd.DataFrame({
'Company': ['Alpha', 'Beta', 'Gamma', 'Delta', 'Epsilon', 'Zeta'],
'Revenue (Cr)': [120, 80, 150, 60, 95, 200],
'Profit (Cr)': [25, 18, 35, 12, 22, 45],
'Employees': [800, 500, 1200, 400, 700, 1500],
'Sector': ['Tech', 'Retail', 'Tech', 'Retail', 'Finance', 'Tech']
})
fig = px.scatter(
df,
x='Revenue (Cr)',
y='Profit (Cr)',
size='Employees',
color='Sector',
hover_name='Company',
size_max=70,
log_x=True, # Better spread for revenue
opacity=0.8,
title='Company Analysis: Revenue vs Profit (Bubble = Headcount)',
labels={
'Revenue (Cr)': 'Annual Revenue (₹ Crore)',
'Profit (Cr)': 'Net Profit (₹ Crore)'
}
)
fig.update_layout(
legend_title="Sector",
font=dict(size=12),
xaxis_title="Revenue (log scale)",
yaxis_title="Profit"
)
fig.show()
Hinglish Tip: log_x=True → chhote aur bade companies dono dikhe — no overlap!
Animated Bubble Chart (Over Time)
df = px.data.gapminder()
fig = px.scatter(
df,
x="gdpPercap", y="lifeExp",
size="pop", color="continent",
hover_name="country",
animation_frame="year",
animation_group="country",
size_max=55,
range_x=[100,100000], range_y=[25,90],
log_x=True,
title="World Development: 1952–2007 (Animated)"
)
fig.show()
Hinglish Tip: animation_frame="year" → time travel — dekho kaise countries improve hote hain!
Bubble with Custom Size Scaling
fig = px.scatter(
df, x='Revenue (Cr)', y='Profit (Cr)',
size='Employees',
size_max=60
)
# Custom size scaling (sqrt for better perception)
import numpy as np
sizes = np.sqrt(df['Employees']) * 2 # Adjust multiplier
fig.update_traces(marker=dict(sizemode='area', sizeref=2.*max(sizes)/(60.**2), sizemin=4))
fig.show()
Common Mistakes to Avoid
| Mistake | Fix |
|---|---|
| Bubbles too big → hide others | Use size_max=50 + opacity=0.7 |
| Linear scale → small bubbles invisible | Use log_x or log_y |
| No hover info | Use hover_name + hover_data |
| Animation jumps | Use animation_group="country" |
| Can't export | Install kaleido |