Scatter Plot
Size Legend Bubble
Bubble chart with proper size legend.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
COLORS = {
'primary': '#8B5CF6',
'background': '#FFFFFF',
'text': '#1E293B',
'text_muted': '#64748B',
'grid': '#F1F5F9',
}
np.random.seed(42)
x = np.random.uniform(1, 9, 25)
y = np.random.uniform(1, 9, 25)
sizes_data = np.random.uniform(10, 100, 25)
sizes = sizes_data * 8
fig, ax = plt.subplots(figsize=(10, 6), dpi=100)
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])
ax.scatter(x, y, s=sizes*1.5, c=COLORS['primary'], alpha=0.1, zorder=1)
ax.scatter(x, y, s=sizes, c=COLORS['primary'], alpha=0.7,
edgecolors='white', linewidths=2, zorder=2)
for size_val in [25, 50, 100]:
ax.scatter([], [], s=size_val*8, c=COLORS['primary'], alpha=0.7,
edgecolors='white', linewidths=2, label=f'{size_val}')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color(COLORS['grid'])
ax.spines['bottom'].set_color(COLORS['grid'])
ax.yaxis.grid(True, color=COLORS['grid'], linewidth=1)
ax.set_axisbelow(True)
ax.tick_params(axis='both', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.12), title='Population (M)',
ncol=3, frameon=False, fontsize=9, labelcolor=COLORS['text_muted'], title_fontsize=9)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Pairwise Data
More Scatter Plot examples
☕