How to label bubble chart/scatter plot with column from Pandas dataframe?

To label bubble charts or scatter plots with data from a Pandas DataFrame column, we use the annotate() method to add text labels at each data point position.

Creating a Labeled Scatter Plot

Here's how to create a scatter plot with labels from a DataFrame column ?

import pandas as pd
import matplotlib.pyplot as plt

# Set figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create a dataframe
df = pd.DataFrame({
    'x': [1, 3, 2, 4, 5],
    'y': [0, 3, 1, 2, 5],
    'labels': ['Kiran', 'Raju', 'Raja', 'Javed', 'Rishi']
})

# Create scatter plot
ax = df.plot.scatter(x='x', y='y', alpha=0.7, s=100)

# Annotate each point with labels
for i, label in enumerate(df.labels):
    ax.annotate(label, (df.x.iat[i] + 0.05, df.y.iat[i]))

plt.title('Labeled Scatter Plot')
plt.show()

Advanced Labeling with Styling

You can customize the appearance of labels with additional parameters ?

import pandas as pd
import matplotlib.pyplot as plt

# Create sample data
df = pd.DataFrame({
    'x': [1, 2, 3, 4, 5],
    'y': [2, 5, 3, 8, 7],
    'size': [20, 50, 30, 80, 60],
    'names': ['A', 'B', 'C', 'D', 'E']
})

# Create bubble chart
plt.figure(figsize=(8, 6))
plt.scatter(df.x, df.y, s=df.size*5, alpha=0.6, c=range(len(df)), cmap='viridis')

# Add styled labels
for i, name in enumerate(df.names):
    plt.annotate(name, 
                (df.x.iat[i], df.y.iat[i]),
                xytext=(5, 5),  # offset from point
                textcoords='offset points',
                fontsize=10,
                fontweight='bold',
                bbox=dict(boxstyle='round,pad=0.3', facecolor='white', alpha=0.7))

plt.title('Styled Bubble Chart with Labels')
plt.xlabel('X Values')
plt.ylabel('Y Values')
plt.show()

Key Parameters

Parameter Description Example
xytext Offset from data point (5, 5)
fontsize Text size 10
bbox Background box style dict(boxstyle='round')
alpha Transparency level 0.7

Conclusion

Use annotate() to add labels from DataFrame columns to scatter plots. Customize with xytext for positioning and bbox for styling to create professional-looking visualizations.

Updated on: 2026-03-26T02:33:34+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements