How to add text to Matplotlib?
Last Updated :
16 Feb, 2022
Matplotlib is a plotting library in Python to visualize data, inspired by MATLAB, meaning that the terms used (Axis, Figure, Plots) will be similar to those used in MATLAB. Pyplot is a module within the Matplotlib library which is a shell-like interface to Matplotlib module.
It provides almost any kind of plot that we can think of. In this post, we will focus on a more specific topic which is adding text on matplotlib plot. The following commands are used to create text in the matplotlib plot.
Commands |
Description |
text |
This is used for adding text at an arbitrary location of the Axes. |
annotate |
This is used for adding an annotation, with an optional arrow, at an arbitrary location of the Axes. |
set_xlabel |
This is used for adding label to the Axes’ x-axis. |
set_ylabel |
This is used for adding label to the Axes’ y-axis. |
set_title |
This is used for adding title to the Axes. |
text |
This is used for adding text at an arbitrary location of the Figure. |
suptitle |
This is used for adding title to the Figure. |
We will see each of the commands one by one, first, let’s create a basic plot of Day v/s Question on which we will add various text objects.
Code:
Python3
import matplotlib.pyplot as plt
x = [ 1 , 2 , 3 , 4 , 5 ]
y = [ 5 , 8 , 4 , 7 , 5 ]
fig = plt.figure()
ax = fig.add_subplot( 111 )
ax.plot(x, y)
plt.show()
|
Output:

The output plot looks very simple. Now, let’s see some text commands to add it on our plot.
- set_title() is used to add the title of axes. The first and mandatory argument is the title you want to give and the rest are optional to format it.
- Similarly, set_xlabel() and set_ylabel() are used to add titles to x-axis and y-axis. It also takes title as an argument.
It is better to adjust the range on y-axis so that we can have some space to add text later on. For this, we will use ax.axis() which allows specifying value ranges (the first two for x-axis and the other two for y-axis).
Now, let’s add its title and names of x-axis and y-axis.
Code:
Python3
ax.set_title( 'Day v/s No of Questions on GFG' , fontsize = 15 )
ax.set_xlabel( 'Day' , fontsize = 12 )
ax.set_ylabel( 'No of Questions' , fontsize = 12 )
ax.axis([ 0 , 10 , 0 , 15 ])
|
Output:

Now, it looks better than the previous version. It’s the time to add text to our plot. First, let’s see about them.
axes.text() is used to add text at an arbitrary location of the Axes. For this we need to specify the location of the text and of course what the text is. For instance, the following code will add “Practice on GFG” text. It will located according to the point whose coordinates are specified ([1,13] in this case). The parameter bbox is used to capture the text with a box. As argument to bbox parameter, we pass a dictionary which includes formatting styles.
Code:
Python3
ax.text( 1 , 13 , 'Practice on GFG' , style = 'italic' , bbox = {
'facecolor' : 'grey' , 'alpha' : 0.5 , 'pad' : 10 })
|
If we want to do not have to box the text then simply do not assign anything to the bbox parameter. The following code adds the specified text without a box.
Code:
Python3
ax.text( 8 , 13 , 'December' , style = 'italic' )
|
We can also add text with annotations.
axes.annotate() is used to add an annotation, with an optional arrow, at an arbitrary location of the Axes. Its xy parameter contains the coordinates for arrow and xytext parameter specifies the location of the text. Arrowprops parameter is used to style the arrow.
For instance, we can mark the peak value of the Day-Question data with an annotation.
Code:
Python3
ax.annotate( 'Peak' , xy = ( 2 , 8 ), xytext = ( 4 , 10 ), fontsize = 12 ,
arrowprops = dict (facecolor = 'green' , shrink = 0.05 ))
|
Let’s put all this together and see the final code.
Code:
Python3
import matplotlib.pyplot as plt
x = [ 1 , 2 , 3 , 4 , 5 ]
y = [ 5 , 8 , 4 , 7 , 5 ]
fig = plt.figure()
ax = fig.add_subplot( 111 )
ax.plot(x, y)
ax.set_title( 'Day v/s No of Questions on GFG' , fontsize = 15 )
ax.set_xlabel( 'Day' , fontsize = 12 )
ax.set_ylabel( 'No of Questions' , fontsize = 12 )
ax.axis([ 0 , 10 , 0 , 15 ])
ax.text( 1 , 13 , 'Practice on GFG' , style = 'italic' , bbox = {
'facecolor' : 'green' , 'alpha' : 0.5 , 'pad' : 10 })
ax.text( 8 , 13 , 'December' , style = 'italic' )
ax.annotate( 'Peak' , xy = ( 2 , 8 ), xytext = ( 4 , 10 ), fontsize = 12 ,
arrowprops = dict (facecolor = 'green' , shrink = 0.05 ))
plt.show()
|
Output:

Similar Reads
How to add a title to a Matplotlib legend?
Prerequisites: Matplotlib In this article, we will see how can we can add a title to a legend in our graph using matplotlib, Here we will take two different examples to showcase our graph. Approach: Import required module.Create data.Add a title to a legend.Normally plot the data.Display plot. Below
2 min read
How to Add Title to Subplots in Matplotlib?
In this article, we will see how to add a title to subplots in Matplotlib? Let's discuss some concepts : Matplotlib : Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work
3 min read
How to update a plot in Matplotlib?
In this article, let's discuss how to update a plot in Matplotlib. Updating a plot simply means plotting the data, then clearing the existing plot, and then again plotting the updated data and all these steps are performed in a loop. Functions Used:canvas.draw(): It is used to update a figure that h
2 min read
How to Create a Table with Matplotlib?
In this article, we will discuss how to create a table with Matplotlib in Python. Method 1: Create a Table using matplotlib.plyplot.table() function In this example, we create a database of average scores of subjects for 5 consecutive years. We import packages and plotline plots for each consecutive
3 min read
How to Plot a Time Series in Matplotlib?
Time series data is the data marked by some time. Each point on the graph represents a measurement of both time and quantity. A time-series chart is also known as a fever chart when the data are connected in chronological order by a straight line that forms a succession of peaks and troughs. x-axis
4 min read
How to Add a Title to Seaborn Plots?
In this article, we will discuss how to add a title to Seaborn Plots in Python. Method 1: Adding title using set methodset method takes 1 argument "title" as a parameter which stores Title of a plot. Syntax set(title="Title of a plot") Example: Here, we are going to create a dataframe with months an
2 min read
How to save Matplotlib Animation?
In this article, we will learn How to save Matplotlib Animation. The animated graphs made with the help of matplotlib can be saved as videos in Python. As we can create captivating animations using the matplotlib library. If you want to learn to create animations, here is a link to the article to cr
2 min read
How to Adjust Title Position in Matplotlib?
In this article, you learn how to modify the Title position in matplotlib in Python. Method 1: Using matplotlib.pyplot.title() function The title() method in matplotlib module is used to specify title of the visualization depicted and displays the title using various attributes. Syntax: matplotlib.p
3 min read
How to add a legend to a scatter plot in Matplotlib ?
In this article, we are going to add a legend to the depicted images using matplotlib module. We will use the matplotlib.pyplot.legend() method to describe and label the elements of the graph and distinguishing different plots from the same graph. Syntax: matplotlib.pyplot.legend( ["title_1", "Title
2 min read
How to Save a Plot to a File Using Matplotlib?
Matplotlib is a popular Python library to create plots, charts, and graphs of different types. show() is used to plot a plot, but it doesn't save the plot to a file. In this article, we will see various methods through which a Matplotlib plot can be saved as an image file. Methods to Save a Plot in
3 min read