0
0
Matplotlibdata~5 mins

Why annotations tell the data story in Matplotlib

Choose your learning style9 modes available
Introduction

Annotations help explain important points in a chart. They make the story behind the data clear and easy to understand.

When you want to highlight a peak or a drop in a graph.
When you need to explain a special event or outlier in the data.
When showing trends that need extra explanation for clarity.
When presenting data to people who may not know the background.
When you want to add notes directly on the chart instead of separate text.
Syntax
Matplotlib
plt.annotate(text, xy, xytext=None, arrowprops=None, **kwargs)

text is the label you want to show.

xy is the point (x, y) to annotate.

Examples
Simple annotation at point (2, 8) with label 'Peak'.
Matplotlib
plt.annotate('Peak', xy=(2, 8))
Annotation with an arrow pointing from text at (5,5) to point (4,2).
Matplotlib
plt.annotate('Drop here', xy=(4, 2), xytext=(5, 5), arrowprops=dict(facecolor='red'))
Sample Program

This code plots sales over 5 days and adds an annotation to highlight the highest sales point with an arrow and label.

Matplotlib
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 8, 5, 2, 7]

plt.plot(x, y, marker='o')
plt.title('Sales over 5 days')
plt.xlabel('Day')
plt.ylabel('Sales')

# Annotate the highest sales point
max_index = y.index(max(y))
plt.annotate('Highest Sales', xy=(x[max_index], y[max_index]), xytext=(x[max_index]+0.5, y[max_index]+1), arrowprops=dict(facecolor='blue', shrink=0.05))

plt.show()
OutputSuccess
Important Notes

Annotations make charts easier to understand by pointing out key data points.

You can customize arrows, text position, and style to fit your story.

Summary

Annotations add clear labels to important points in charts.

They help tell the story behind the data visually.

Use arrows and text to guide the viewerโ€™s attention.