Add Text Inside the Plot in Matplotlib
Last Updated :
11 Jan, 2024
In this article, we are going to see how to add text inside the plot in Matplotlib. The matplotlib.pyplot.text() function is used to add text inside the plot. The syntax adds text at an arbitrary location of the axes. It also supports mathematical expressions.
Python matplotlib.pyplot.text() Syntax
Syntax: matplotlib.pyplot.text(x, y, s, fontdict=None, **kwargs)
Add Text Inside the Plot in Matplotlib
Below are some examples by which we can add the Matplotlib text inside the plot in Python:
- Adding Mathematical Equations
- Adding a Rectangular box Around the Text
- Adding the Text "Sine wave"
- Using Annotation Along with text
Adding Mathematical Equations as Text Inside the Plot
In this example, this code uses Matplotlib and NumPy to generate a plot of the parabolic function y = x^2 over the range -10 to 10. The code adds a text label "Parabola $Y = x^2$" at coordinates (-5, 60) within the plot. Finally, it sets axis labels, plots the parabola in green, and displays the plot.
Python3
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-10, 10, 0.01)
y = x**2
#adding text inside the plot
plt.text(-5, 60, 'Parabola $Y = x^2$', fontsize = 22)
plt.plot(x, y, c='g')
plt.xlabel("X-axis", fontsize = 15)
plt.ylabel("Y-axis",fontsize = 15)
plt.show()
Output:

Matplotlib Text Using Rectangular box Around the Text
In this example, the code uses Matplotlib and NumPy to create a plot of the parabolic function "y = x^2" for the range -10 to 10 with a step size of 0.01. It labels the X and Y axes, adds a text label "Parabola "Y = x^2" in a red, semi-transparent box at coordinates (-5, 60) and finally plots the parabola in green, displaying the resulting plot.
Python3
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-10, 10, 0.01)
y = x**2
plt.xlabel("X-axis", fontsize = 15)
plt.ylabel("Y-axis",fontsize = 15)
#Adding text inside a rectangular box by using the keyword 'bbox'
plt.text(-5, 60, 'Parabola $Y = x^2$', fontsize = 22,
bbox = dict(facecolor = 'red', alpha = 0.5))
plt.plot(x, y, c = 'g')
plt.show()
Output:

Add Text "Sine wave" on a Figure in Matplotlib
In this example, the code uses Matplotlib and NumPy to create a sine wave plot. It generates x values from 0 to 10 with a step of 0.1, calculates corresponding sine values, and plots the sine wave. this code also adds a text label, sets axis labels, and displays the plot. The grid line command is commented out but can be uncommented to show a grid on the plot.
Python3
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.1)
y = np.sin(x)
plt.plot(x,y)
plt.text(3.5, 0.9, 'Sine wave', fontsize = 23)
plt.xlabel('X-axis', fontsize = 15)
plt.ylabel('Y-axis', fontsize = 15)
#plt.grid(True, which='both')
plt.show()
Output:Â

Add Text on a Figure in Matplotlib Using Annotation
In this example, the code uses Matplotlib to create a bar chart representing the marks of students. Names of students ('x') are on the x-axis, and their corresponding marks ('y') are on the y-axis. The code adds a title, labels for the x and y axes, and an annotation indicating the student with the highest score, pointing to the corresponding bar with a red arrow. Finally, it displays the chart using `plt.show()`.
Python3
import matplotlib.pyplot as plt
import numpy as np
x = ['Rani', 'Meena', 'Raju', 'Jhansi', 'Ram']
y = [5, 7, 9, 2, 6]
plt.bar(x,y)
plt.text(3, 7, 'Student Marks',
fontsize = 18, color = 'g')
plt.xlabel('Students', fontsize = 15)
plt.ylabel('Marks', fontsize = 15)
plt.annotate('Highest scored', xy = (2.4, 8),
fontsize = 16, xytext = (3, 9),
arrowprops = dict(facecolor = 'red'),
color = 'g')
plt.show()
Output:
Similar Reads
How to use matplotlib plot inline? Matplotlib is a Python library that helps in drawing graphs. It is used in data visualization and graph plotting. Matplotlib Plot Inline is a package that supports Matplotlib to display plots directly inline and save them to notebooks. In this article, we'll cover the following:Â What is Matplotlib I
3 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
1 min read
How to add a legend to a scatter plot in Matplotlib ? Adding a legend to a scatter plot in Matplotlib means providing clear labels that describe what each group of points represents. For example, if your scatter plot shows two datasets, adding a legend will display labels for each dataset, helping viewers interpret the plot correctly. We will explore s
2 min read
How to Add an Average Line to Plot in Matplotlib In this article, we will learn how we can add an average line to a plot in matplotlib. We will discuss the steps to add a horizontal average line using the axhline function, as well as the steps to add a vertical average line using the axvline function in Matplotlib. Throughout the article, we will
5 min read