Complete Guide to Annotating Scatter Plots with Different Text Using Matplotlib

Nov 02, 2025 · Programming · 15 views · 7.8

Keywords: Python | Matplotlib | Scatter Plot | Data Annotation | Data Visualization

Abstract: This article provides a comprehensive guide on using Python's Matplotlib library to add different text annotations to each data point in scatter plots. Through the core annotate() function and iterative methods, combined with rich formatting options, readers can create clear and readable visualizations. The article includes complete code examples, parameter explanations, and practical application scenarios.

Introduction

Scatter plots are widely used in data visualization to intuitively display relationships between two variables. In practical applications, merely showing the positions of data points is often insufficient to convey complete information. Adding corresponding text annotations to each data point can significantly enhance the readability and informational value of charts. Python's Matplotlib library provides powerful annotation capabilities that can meet various complex data labeling requirements.

Core Method: The annotate() Function

The annotate() function in Matplotlib is the core tool for implementing data point annotations. This function allows adding text annotations at specified coordinate positions and provides rich formatting options to control the appearance and placement of annotations.

Basic Implementation Code

The following code demonstrates how to use the annotate() function to add different text annotations to each data point in a scatter plot:

import matplotlib.pyplot as plt

# Define sample data
x = [0.15, 0.3, 0.45, 0.6, 0.75]
y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
n = [58, 651, 393, 203, 123]

# Create figure and axes
fig, ax = plt.subplots(figsize=(10, 6))

# Create scatter plot
ax.scatter(x, y, color='blue', marker='o', s=50)

# Add text annotations for each data point
for i, txt in enumerate(n):
    ax.annotate(str(txt), (x[i], y[i]), 
                xytext=(5, 5), textcoords='offset points',
                fontsize=10, ha='left', va='bottom')

# Set axis labels and title
ax.set_xlabel('X Variable')
ax.set_ylabel('Y Variable')
ax.set_title('Scatter Plot with Annotations')

# Display grid
ax.grid(True, linestyle='--', alpha=0.7)

plt.tight_layout()
plt.show()

Code Analysis

In the above code, we first import the necessary Matplotlib library, then define three sets of data: x-coordinates, y-coordinates, and corresponding annotation texts. The plt.subplots() method creates figure and axes objects, and the scatter() method draws the basic scatter plot.

The key part lies in the annotate() call within the for loop:

Advanced Formatting Options

The annotate() function provides multiple parameters for fine-grained control over annotation appearance:

# Advanced annotation example
for i, txt in enumerate(n):
    ax.annotate(f'ID: {txt}', (x[i], y[i]),
                xytext=(10, 10), textcoords='offset points',
                bbox=dict(boxstyle='round,pad=0.3', facecolor='yellow', alpha=0.7),
                arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0',
                              color='red', lw=1.5),
                fontsize=12, fontweight='bold', color='darkblue',
                ha='center', va='center')

In this advanced example:

Alternative Method: The text() Function

Besides annotate(), Matplotlib also provides the text() function for simple text annotations:

# Using text() function for annotations
for i, txt in enumerate(n):
    ax.text(x[i], y[i] + 0.05, str(txt), 
            fontsize=9, ha='center', va='bottom',
            color='darkred', style='italic')

The text() function is simpler and more direct, suitable for scenarios that don't require complex elements like arrows, but has relatively limited functionality.

Handling Annotation Overlap

When data points are dense, annotations may overlap each other. The adjustText library can be used to automatically adjust annotation positions:

from adjustText import adjust_text

# Create list of annotation texts
texts = []
for i, txt in enumerate(n):
    text = ax.annotate(str(txt), (x[i], y[i]), 
                      xytext=(5, 5), textcoords='offset points')
    texts.append(text)

# Automatically adjust annotation positions to avoid overlap
adjust_text(texts, arrowprops=dict(arrowstyle='->', color='gray'))

Practical Application Scenarios

Data point annotations have important applications in multiple fields:

Best Practice Recommendations

To create effective annotated scatter plots, we recommend:

Conclusion

Through Matplotlib's annotate() function, we can easily add custom text annotations to each data point in scatter plots. Combined with rich formatting options and advanced techniques, we can create both aesthetically pleasing and information-rich visualizations. Mastering these techniques is of significant importance for data analysis and result presentation.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.