Open In App
Related Articles

Add Text Inside the Plot in Matplotlib

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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:



Last Updated : 11 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads