Add Text to the Corner and Center of a Plot in Matplotlib

When I first started working with Matplotlib in Python, I often wanted to label my plots with quick notes, maybe to show the dataset source, highlight a key metric, or display a company name in the corner.

But here’s the thing, while Matplotlib gives us powerful tools for visualization, positioning text exactly where we want it (like in the corners or center of the plot) can be tricky if you don’t know the right methods.

I’ve learned some simple and effective ways to add text to the corner and center of a Plot in Matplotlib, whether it’s in one of the four corners or right at the center of the plot.

Add Text to the Corner of a Plot in Matplotlib (Python)

When you’re creating charts in Python, adding text to the corners of a plot can help display additional information like a copyright notice, data source, or a short note.

I’ll show you two easy methods to place text in the corners using Matplotlib’s text() function and relative coordinates.

Method 1 – Use plt.text() with Axes Coordinates in Matplotlib

In this method, I’ll use the plt.text() function and specify the position using normalized coordinates (ranging from 0 to 1).

This approach is simple and works perfectly when you want the text to stay fixed in the same position, even if the data range changes.

Here’s how you can do it:

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 15, 13, 18, 16]

plt.plot(x, y, marker='o', color='green')
plt.title("Sales Growth Over 5 Years (USA)")

# Add text to the top-left corner
plt.text(0.02, 0.95, "Top Left Corner", transform=plt.gca().transAxes,
         fontsize=12, color='blue', ha='left', va='top')

# Add text to the bottom-right corner
plt.text(0.98, 0.05, "Bottom Right Corner", transform=plt.gca().transAxes,
         fontsize=12, color='red', ha='right', va='bottom')

plt.xlabel("Year")
plt.ylabel("Sales (in $1000s)")
plt.grid(True)
plt.show()

You can refer to the screenshot below to see the output.

Add Text to the Corner of Plot in Matplotlib

In the code above, I used the transform=plt.gca().transAxes parameter, which tells Matplotlib to interpret the coordinates relative to the axes (0 means left/bottom, 1 means right/top).

This is one of my favorite ways to add corner text because it stays consistent even when you zoom or resize the plot.

Method 2 – Use ax.annotate() for More Control in Matplotlib

If you want more flexibility, like adding arrows, adjusting offsets, or controlling alignment, the annotate() method in Matplotlib is even better.

Let me show you how to use it to add text to a corner.

import matplotlib.pyplot as plt

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

# Plot some data
x = [0, 1, 2, 3, 4]
y = [2, 3, 5, 7, 6]
ax.plot(x, y, color='purple', linewidth=2)

# Add corner text using annotate
ax.annotate("Top Right Corner",
            xy=(1, 1), xycoords='axes fraction',
            xytext=(-10, -10), textcoords='offset points',
            ha='right', va='top', fontsize=12, color='darkgreen')

# Add another text to bottom-left
ax.annotate("Bottom Left Corner",
            xy=(0, 0), xycoords='axes fraction',
            xytext=(10, 10), textcoords='offset points',
            ha='left', va='bottom', fontsize=12, color='orange')

ax.set_title("Website Traffic Trends (Python Data)")
ax.set_xlabel("Month")
ax.set_ylabel("Visitors (in thousands)")
plt.show()

You can refer to the screenshot below to see the output.

Matplotlib Add Text to the Corner of a Plot

In this method, I used xycoords=’axes fraction’, which works similarly to transAxes. The xytext parameter lets me adjust the text position slightly using pixel offset, ideal for fine-tuning text placement.

If you’re building a dashboard or a report in Python, this method gives you precise control over how your annotations look.

Add Text to the Center of a Plot in Matplotlib (Python)

Sometimes, you may want to place text right in the middle of your plot, for example, to display a percentage, a label, or a “No Data Available” message.

I’ll show you two simple Python methods to add text to the center using Matplotlib.

Method 1 – Use plt.text() with Center Alignment

This is the easy way to add centered text. You just pass the coordinates of the center and set the alignment to ‘center’ for both horizontal and vertical placement.

Here’s a practical example:

import matplotlib.pyplot as plt

# Create a simple bar chart
categories = ['Q1', 'Q2', 'Q3', 'Q4']
values = [150, 200, 180, 220]

plt.bar(categories, values, color='skyblue')
plt.title("Quarterly Revenue (in $1000s)")

# Add text to the center of the plot
plt.text(0.5, 0.5, "Center Text Example",
         transform=plt.gca().transAxes,
         fontsize=14, color='darkred',
         ha='center', va='center', alpha=0.8)

plt.xlabel("Quarter")
plt.ylabel("Revenue")
plt.show()

You can refer to the screenshot below to see the output.

Add Text to the Center of Plot in Matplotlib

Here, the coordinates (0.5, 0.5) represent the exact center of the plot in normalized axes coordinates. This method is perfect when you want the text to remain fixed at the visual center, regardless of the data range.

Method 2 – Use ax.text() with Dynamic Center Calculation

In some cases, you may want to add text dynamically at the data center instead of the visual center. For example, if your dataset changes, you might want the text to appear in the middle of the plotted data.

Here’s how you can calculate the center and place the text there:

import matplotlib.pyplot as plt
import numpy as np

# Generate some random data
x = np.linspace(0, 10, 50)
y = np.sin(x)

fig, ax = plt.subplots()
ax.plot(x, y, color='teal', linewidth=2)
ax.set_title("Sine Wave Plot - Python Example")

# Calculate data center
x_center = (x.min() + x.max()) / 2
y_center = (y.min() + y.max()) / 2

# Add text at the data center
ax.text(x_center, y_center, "Center of Data",
        fontsize=13, color='black', ha='center', va='center',
        bbox=dict(facecolor='lightyellow', edgecolor='gray', boxstyle='round,pad=0.5'))

ax.set_xlabel("X Axis")
ax.set_ylabel("Y Axis")
plt.grid(True)
plt.show()

You can refer to the screenshot below to see the output.

Matplotlib Add Text to the Center of a Plot

In this Python example, I computed the midpoint of both the x and y data ranges and added text at that location. This approach is great when the plot’s shape or scale changes and you want the text to adjust automatically.

Pro Tips for Adding Text in Matplotlib (Python)

Over the years, I’ve picked up a few tricks that make text placement in Matplotlib even smoother:

  • Use transform=plt.gca().transAxes when you want text to stay fixed relative to the plot area.
  • Use ax.text() when you want text to move dynamically with the data.
  • Add background boxes using the bbox parameter to make text more readable.
  • Combine f-strings with plt.text() to add dynamic values like averages or percentages.
  • Always adjust alignment (ha, va) to ensure the text looks visually balanced.

These small tweaks can make your Matplotlib plots look more professional and polished.

Adding text to a plot in Matplotlib using Python is a small but powerful way to make your visualizations more informative and engaging. Whether you’re labeling the corners with notes or placing a value at the center of your chart, these techniques will give you full control over text positioning.

Personally, I use the plt.text() method for quick annotations and ax.annotate() when I need precision or interactivity. Once you start using these methods, you’ll see how easy it is to create clean, professional-looking visualizations that communicate insights clearly.

You may also like to read:

Leave a Comment

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.