Add Multiple Line Text to a Plot in Matplotlib

I was working on a Python project for visualizing sales data across different U.S. states, and I needed to annotate my Matplotlib plots with multi-line text. The challenge was that the default plt.text() function in Matplotlib only displays a single line unless you format it properly.

I realized that many Python developers, especially those new to data visualization, often struggle to add multi-line annotations or titles to their plots. So, I decided to share a few simple methods that I personally use to add multiple lines of text to a Matplotlib plot.

In this tutorial, I’ll walk you through several easy-to-understand methods to add multi-line text using Python’s Matplotlib library. You’ll learn how to use newline characters, multi-line titles, and even text boxes to make your plots more informative and visually appealing.

Method 1 – Add Multiple-Line Text Using \n in Matplotlib

The simplest way to add multiple lines of text to a plot in Python’s Matplotlib is by using the newline character \n. This method is quick and works perfectly for short annotations or titles.

When I first discovered this, it felt like a hidden gem, no extra imports or functions needed. Just a simple \n between lines, and Matplotlib handles the rest.

Here’s how you can do it:

import matplotlib.pyplot as plt

# Sample data
years = [2018, 2019, 2020, 2021, 2022]
sales = [35000, 42000, 48000, 52000, 61000]

# Create a simple line plot
plt.plot(years, sales, marker='o', color='green')

# Add multiple line text using \n
plt.text(2018.5, 55000, 'Sales Growth\nAcross the USA\n(2018–2022)', 
         fontsize=12, color='blue', ha='left')

# Add labels and title
plt.title('Annual Sales Data')
plt.xlabel('Year')
plt.ylabel('Sales ($)')
plt.grid(True)

plt.show()

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

Multiple Line Text to a Plot in Matplotlib

In the above Python code, I used \n to break the text into three lines. The ha=’left’ parameter ensures the text aligns neatly on the left side.

This method is perfect when you want to quickly annotate a plot with clear, multi-line text that doesn’t require complex formatting.

Method 2 – Add Multi-Line Titles Using plt.suptitle()

Sometimes, you may want to create a multi-line plot title instead of adding text inside the plot area. In that case, Python’s Matplotlib provides the plt.suptitle() function, which supports multi-line text directly.

I often use this approach when I want to add a detailed explanation or subtitle to a chart, especially when presenting reports to clients or stakeholders.

Here’s an example:

import matplotlib.pyplot as plt

# Data for demonstration
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
revenue = [12000, 15000, 18000, 21000, 25000, 27000]

# Create a bar chart
plt.bar(months, revenue, color='orange')

# Add multi-line title using suptitle
plt.suptitle('Monthly Revenue Report\nfor U.S. Retail Stores\n(First Half of 2025)', 
             fontsize=14, fontweight='bold', color='darkred')

# Add axis labels
plt.xlabel('Month')
plt.ylabel('Revenue ($)')
plt.grid(axis='y', linestyle='--', alpha=0.6)

plt.show()

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

Add Multiple Line Text to a Plot in Matplotlib

In this Python example, I used plt.suptitle() with newline characters to create a clean, multi-line title. This approach is ideal when you want your title to stand out above the chart, especially for presentations or dashboards.

Method 3 – Add Multi-Line Text Inside a Text Box

When working on professional data visualizations, I often need to highlight key insights directly on the plot. A great way to do this is by placing multi-line text inside a text box.

Matplotlib’s bbox parameter in plt.text() allows you to create visually distinct text boxes with background colors and borders.

Here’s how I do it:

import matplotlib.pyplot as plt

# Data for demonstration
quarters = ['Q1', 'Q2', 'Q3', 'Q4']
profits = [15000, 22000, 18000, 30000]

# Create a plot
plt.plot(quarters, profits, marker='o', color='purple', linewidth=2)

# Add multi-line text inside a text box
plt.text(0.5, 25000, 'Strong Performance\nin Q2 and Q4\nDriven by Online Sales', 
         fontsize=11, color='white', ha='center', 
         bbox=dict(facecolor='darkblue', alpha=0.7, boxstyle='round,pad=0.5'))

# Add labels and title
plt.title('Quarterly Profit Analysis (2025)')
plt.xlabel('Quarter')
plt.ylabel('Profit ($)')
plt.grid(True, linestyle='--', alpha=0.5)

plt.show()

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

Add Multiple Line Text to a Plot Matplotlib

In this Python example, the bbox dictionary defines the text box’s background color, transparency, and shape. This method is particularly useful when you want to emphasize specific insights or commentary on the chart itself.

Method 4 – Add Multi-Line Text Using ax.text() in Object-Oriented Matplotlib

If you prefer the object-oriented approach (which I use for most of my professional Python projects), you can use the ax.text() method. It gives you more control over positioning and styling.

Here’s a practical example:

import matplotlib.pyplot as plt

# Create figure and axis
fig, ax = plt.subplots(figsize=(7, 4))

# Sample data
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
visitors = [230, 310, 280, 350, 400]

# Plot data
ax.plot(days, visitors, marker='o', color='teal', linewidth=2)

# Add multiple line text using ax.text()
ax.text(0.8, 370, 'Peak Traffic\nObserved on Friday\nDue to Promotions', 
        fontsize=11, color='black', ha='left', 
        bbox=dict(facecolor='lightyellow', edgecolor='gray', boxstyle='round,pad=0.4'))

# Add title and labels
ax.set_title('Daily Website Visitors (U.S. Region)')
ax.set_xlabel('Day of Week')
ax.set_ylabel('Number of Visitors')

plt.tight_layout()
plt.show()

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

Add Multiple Line Text to Plot Matplotlib

This method is perfect when you’re building complex multi-plot dashboards or subplots in Python. The ax.text() function works similarly to plt.text() but gives you precise control within a specific subplot.

Method 5 – Add Multi-Line Text Using plt.annotate()

Another powerful way to add multi-line text is by using plt.annotate(). This function not only allows multi-line text but also lets you draw arrows pointing to specific data points.

Here’s how I use it in my Python visualizations:

import matplotlib.pyplot as plt

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

# Create a scatter plot
plt.scatter(x, y, color='red')

# Annotate with multi-line text
plt.annotate('Notice the sharp\nincrease after Q3\n(Strong market demand)', 
             xy=(4, 22), xytext=(2, 27),
             fontsize=11, color='black',
             arrowprops=dict(facecolor='gray', shrink=0.05, width=1, headwidth=8))

# Add title and labels
plt.title('Quarterly Demand Trend (Python Visualization)')
plt.xlabel('Quarter')
plt.ylabel('Demand Index')

plt.show()

This approach is especially effective when you want to draw attention to a specific data point or event in your plot. The arrow makes it easy for viewers to connect the text to the exact spot on the chart.

Tips for Formatting Multi-Line Text in Matplotlib

Here are a few additional tips I’ve learned over the years while working with Matplotlib in Python:

  • Use font size and font weight to make the text readable and professional.
  • Combine horizontal alignment (HA) and vertical alignment (VA) for perfect text placement.
  • Use bbox for emphasis when presenting to clients or in dashboards.
  • For consistent branding, stick to one or two font colors across all your plots.

Adding multiple-line text to plots in Matplotlib may seem tricky at first, but once you understand how newline characters, annotations, and text boxes work, it becomes second nature.

In my 10+ years of working with Python for data visualization, I’ve found that well-placed text annotations can turn a simple chart into a powerful storytelling tool. Whether you’re reporting quarterly profits, analyzing trends, or building dashboards, multi-line text helps your viewers quickly grasp the key message.

So next time you create a Matplotlib plot, try one of these methods to add clear, professional, and multi-line text annotations. It’s a small detail that can make a big difference in how your data is perceived.

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.