Saturday, June 13, 2026
HomeLanguagesMatplotlib.pyplot.text() function in Python

Matplotlib.pyplot.text() function in Python

This function is used to add a text to the axes at location x, y in data coordinates.

Syntax: matplotlib.pyplot.text(x, y, s, fontdict=None, **kwargs)
parameters                                                                                           Description
x, y:float The position to place the text. By default, this is in data coordinates. The coordinate system can be changed using the transform parameter.
s :str The text.
fontdict : dict default none A dictionary to override the default text properties. If fontdict is None, the defaults are determined by rcParams.
**kwargs Text properties.

Example #1: Text on plot sheet

Python3




import matplotlib.pyplot
  
matplotlib.pyplot.text(0.5, 0.5, "Hello World!")
matplotlib.pyplot.savefig("out.png")


Output: 

Example #2: Add text to a plot

Python3




import matplotlib.pyplot as plt
  
  
w = 4
h = 3
d = 70
  
plt.figure(figsize=(w, h), dpi=d)
  
x = [1, 2, 4]
x_pos = 0.5
y_pos = 3
  
plt.text(x_pos, y_pos, "text on plot")
plt.plot(x)
plt.savefig("out.png")


Output:

RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS