Add Text to a Plot in Matplotlib in Python

I’ve often found that adding text annotations to plots is necessary for making data visualizations clear and insightful. Matplotlib, one of the most popular plotting libraries in Python, offers flexible ways to add text to your plots.

In this tutorial, I’ll share easy methods to add text, based on real-world scenarios, so you can apply them immediately in your projects.

Let’s get in!

Methods to Add Text to a Plot in Matplotlib in Python

Let me show you the easy methods to Add Text to a Plot in Matplotlib in Python.

Read Matplotlib Not Showing Plot

1: Use `plt.text()` to Add Text at Specific Coordinates

The most direct way to add text is with the `text()` function from Matplotlib’s pyplot module in Python.

Here’s how it works:

import matplotlib.pyplot as plt

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales = [25000, 30000, 28000, 35000, 40000]

plt.plot(months, sales, marker='o')

# Add text annotation at a specific point
plt.text(3, 35000, 'Sales peak in April', fontsize=12, color='red')

plt.title('Monthly Sales Data')
plt.xlabel('Month')
plt.ylabel('Sales in USD')
plt.show()

I executed the above example code and added the screenshot below.

python add text to plot

In this example, I placed a note at the coordinate (3, 35000) that corresponds to April’s sales peak. The fontsize and color parameters help make the text stand out.

Coordinates in plt.text() relate to the data points on the axes, so you can precisely position text anywhere on the plot.

2: Use annotate() for More Detailed Annotations

annotate() is more powerful and flexible than text() in Python. It lets you add arrows pointing to data points, which is great for highlighting specific details.

Here’s an example:

plt.plot(months, sales, marker='o')

# Annotate the highest sales point with an arrow
max_sales = max(sales)
max_month = months[sales.index(max_sales)]

plt.annotate('Highest Sales',
             xy=(max_month, max_sales),  # Point to annotate
             xytext=(max_month, max_sales + 5000),  # Text location
             arrowprops=dict(facecolor='green', shrink=0.05),
             fontsize=12, color='green')

plt.title('Monthly Sales Data')
plt.xlabel('Month')
plt.ylabel('Sales in USD')
plt.show()

I executed the above example code and added the screenshot below.

add text to plot python

This method places an arrow pointing to the highest sales month, with the annotation text positioned above it. It’s a neat way to draw attention without cluttering the graph.

Check out Matplotlib Multiple Plots

3: Add Text Inside or Outside the Plot Area

Sometimes, you want to add text outside the plot area, such as titles or notes.

You can do this using plt.figtext() to place text relative to the figure coordinates (from 0 to 1):

plt.plot(months, sales, marker='o')

plt.figtext(0.5, 0.01, 'Data collected from USA regional stores', ha='center', fontsize=10, style='italic')

plt.title('Monthly Sales Data')
plt.xlabel('Month')
plt.ylabel('Sales in USD')
plt.show()

I executed the above example code and added the screenshot below.

plt text

This places a note centered below the plot, perfect for footnotes or data source attribution.

Read Matplotlib Legend Font Size

4: Customize Text Appearance

Matplotlib lets you customize text extensively. You can change font size, color, style, weight, and more.

Example:

plt.plot(months, sales, marker='o')

plt.text(1, 30000, 'Steady growth', fontsize=14, color='blue', fontweight='bold', style='italic')

plt.title('Monthly Sales Data')
plt.xlabel('Month')
plt.ylabel('Sales in USD')
plt.show()

This flexibility helps match your plot’s style to your presentation or report theme.

5: Use Loop to Add Multiple Text Annotations

If you want to label every data point, you can loop through your data:

plt.plot(months, sales, marker='o')

for i, value in enumerate(sales):
    plt.text(i, value + 1000, f'${value}', ha='center', fontsize=9)

plt.title('Monthly Sales Data')
plt.xlabel('Month')
plt.ylabel('Sales in USD')
plt.show()

This approach adds the sales value above each point, making the chart very informative.

Adding text to your plots in Matplotlib is easy but powerful. Whether you want simple labels or detailed annotations with arrows, these methods help make your visualizations clearer and more engaging.

Try these techniques in your next project, especially when presenting data to clients or team members who may not be familiar with the raw numbers. Clear annotations can make all the difference.

You may like to read other Matplotlib tutorials:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.