Open In App

Text box with line wrapping in Matplotlib

Improve
Improve
Like Article
Like
Save
Share
Report

Matplotlib is a majorly used python comprehensive library for visualization(static, animated, creative & interactive). It provides a wide range of methods for annotating and describing the plot through text. Also, it can wrap the text automatically.

Text is used in a plot for various purposes Aside from describing the plot, the other use for text is to provide general notes and the reader’s attention. Matplotlib provides methods to add text to a plot.

Approach

  • Import module
  • Create data
  • Plot data
  • Add text
  • Display plot
  • text() : This method provides general text to the graph. It adds the text to an arbitrary location.

Syntax : 

matplotlib.pyplot.text(x, y, string, fontdict, withdash, **kwargs)

Example :

Python3




import matplotlib.pyplot as plt
  
  
x = [1, 2, 3, 4, 5]
y = [50, 40, 40, 80, 20]
y2 = [80, 20, 20, 50, 60]
  
plt.plot(x, y, 'g', label='BMW', linewidth=5)
plt.plot(x, y2, 'c', label='Ferrari', linewidth=5)
  
plt.title('Car details in line plot')
plt.ylabel('Distance in kms')
plt.xlabel('Days')
  
# Text on Ferrari line plot
plt.text(2.5, 23, "Ferrari")
  
# Text on BMW line plot
plt.text(2.5, 43, "BMW")
plt.legend()


Output :

  • figtext() : figtext() can be used as an alternative of text() method. figtext() helps to place text in any location on the figure. We can also put text outside the Axes of the plot.

Syntax :

 matplotlib.pyplot.figtext(x, y, string, fontdict=None, **kwargs)

Example :

Python3




import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [50, 40, 40, 80, 20]
y2 = [80, 20, 20, 50, 60]
  
plt.plot(x, y, 'g', label='BMW', linewidth=5)
plt.plot(x, y2, 'c', label='Ferrari', linewidth=5)
  
plt.title('Car details in line plot')
plt.ylabel('Distance in kms')
plt.xlabel('Days')
plt.figtext(0.4, 0.2, "Ferrari")  
plt.figtext(0.35, 0.4, "BMW")  
plt.legend()


Output :

  • annotation(): With the help of these methods we can annotate a point on a plot  by adding an arrow.

Syntax :

  matplotlib.pyplot.annotate( string, xy, xytext, arrowprops, **kwargs)

Example 1:

Python3




import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [50, 40, 40, 80, 20]
y2 = [80, 20, 20, 50, 60]
  
plt.plot(x, y, 'g', label='BMW', linewidth=5)
plt.plot(x, y2, 'c', label='Ferrari', linewidth=5)
  
plt.title('Car details in line plot')
plt.ylabel('Distance in kms')
plt.xlabel('Days')
  
# Text on Ferrari line plot
plt.annotate('BMW', xy=(2.5, 40), xytext=(3, 55), arrowprops=dict(
               width=1, headwidth=8, facecolor='black', shrink=0.05))  
  
plt.legend()


Output :

Example 2: Text box with line wrapping using pyplot.text() :

Python3




import matplotlib.pyplot as plt
  
fig = plt.figure()
plt.axis([0, 10, 0, 10])
t = ("Welcome to GeeksforGeeks")
plt.text(5, 8, t, fontsize=18, style='oblique', ha='center',
         va='top', wrap=True)
  
plt.show()


Output :

 We can rotate the text by using rotation parameter.

Example 3:

Python3




import matplotlib.pyplot as plt
  
fig = plt.figure()
plt.axis([0, 10, 0, 10])
t = ("Welcome to GeeksforGeeks")
  
plt.text(5, 8, t, fontsize=18, rotation=15, style='oblique', ha='center',
         va='top', wrap=True# rotate the text 15 degree.
  
plt.show()


Output :



Last Updated : 24 Feb, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads