Introduction
Annotations help explain important points in a chart. They make the story behind the data clear and easy to understand.
Annotations help explain important points in a chart. They make the story behind the data clear and easy to understand.
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.
plt.annotate('Peak', xy=(2, 8))
plt.annotate('Drop here', xy=(4, 2), xytext=(5, 5), arrowprops=dict(facecolor='red'))
This code plots sales over 5 days and adds an annotation to highlight the highest sales point with an arrow and label.
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()
Annotations make charts easier to understand by pointing out key data points.
You can customize arrows, text position, and style to fit your story.
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.
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.annotate('Peak', xy=(3, 6), xytext=(2, 5), arrowprops=dict(facecolor='black'))
plt.show()plt.plot([1, 2, 3], [3, 2, 1])
plt.annotate('Low', xytext=(3, 3), (2, 2))
plt.show()Pass the test and this pattern moves unknown