How to Add Text on a Figure in Matplotlib?

Learn via video courses
Topics Covered

Overview

Making our plots and visualizations presentable is one of Python's critical aspects of data visualizations. One of the most prominent ways to make your plots informative is to add text in the right places. Suppose we have to plot the prices of houses in a particular year. To represent the house with the highest price, we can either fill the bar for that house, or we can use text to indicate the prices of the houses. In this way, we can explain our data better. This article will cover various ways to add texts to our plots, along with some examples.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

How to Add Text to a Plot?

In matplotlib, there are many ways to add texts to a plot. The easiest and most feasible way is using the plt.text() function.

plt.text() function is widely preferred when putting texts on plots, as it is highly customizable and very easy to understand.

In matplotlib, we have total control over every text property (font size, weight, location, colour, and so on).

Some pyplot functions that can be used to insert text in our plots are: text(), xlabel(), ylabel(), title() etc. All these commands return a matplotlib.text.Text instance.

We can customize the matplotlib text properties by passing keyword arguments to the functions or using matplotlib.artist.setp().

Syntax

Where:

  • x_c: x-coordinate where we want our text to be.
  • y_c: y-coordinate where we want our text to be.
  • s: String that we want to be displayed.

Here's an example to start with plt.text()!

Output:

add-text-to-plot

Alternatively, we can also use the LaTex syntax. What is a LaTex syntax? Don't worry, we've got you covered!

LaTex is a system used to produce documents with a professional appearance. Based on the WYSIWYM (what you see is what you mean) principle, you only need to concentrate on the content of your matplotlib text; the computer will handle the formatting.

Output:

add-text-to-plot-using-latex

Adding multi-line Text to a Plot

We need to use the '\n' keyword to add multi-line text to a plot. '\n' in Python adds a new line to our string. We require multi-line text to a plot when we want to label a data point in a detailed manner. We will use it similar way in our plot.

Output:

add-multi-line-text-to-plot

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

How to Change the Style of a Text?

Changing the style of texts in a plot is important because not every text can be visible in a plot. Using a large font will be insignificant if our plot is of high density. Inversely, using a small font would be meaningless if our plot is of low density.

In matplotlib, there are many options for studying our texts. For example, we could increase the font size and the style of the font. We could also increase the intensity of the font.

In this example, we will try to centre our text to the centre, using the horizontal and vertical arguments.

Output:

add-text-to-centre

To change the colour of our text in matplotlib, we use color parameter in plt.text()

Output:

setting-text-on-top-left-corner

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Examples

Here are additional examples to strengthen your hold on matplotlib text.

Setting text on the top-left corner:

Output:

setting-text-on-top-left-corner

Similarly, we can move our matplotlib text anywhere in our plot!

Setting texts in the margin

Output:

setting-text-in-margin

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Conclusion

  • This article covered different ways to write texts on our plots.
  • Apart from that, we understood various methods to change the style of our matplotlib text in the plot.
  • We learned how to create multi-line texts in our plots.
  • We also looked at examples in which we wrote a text on the margins of our plots, etc.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more