Matplotlib: Working With Text

Matplotlib also support text, including mathematical expressions, also TrueType support  for raster and vextor outputs, newline separated text with arbitrary. 

The user can custromrise the text according his choice. We can change front size, font weight, text location and color, etc. User has a great control over text properties.

Matplotlib also implements a large number of Tex math symbols and commands  that provide support to mathematical expression anywhere in figure.

Matplotlib is having it's own function that can implement a cross-platform, W3C compliant font finding algorithm. Matplotlib Text is a tool that allows us ton add text to our graph . It is very easy to add text on axis and any frames. 

Here are some basic text commands which we can use to add text on graphs:

  1. text: This command is used to add text at an arbitrary location of the Axes.
  2. annotate: It is used to add annotation at  an arbitrary location of the Axes with the help of an arrow(which is optional).
  3. xlabel: It is used to add a label to the Axes’s x-axis.
  4. ylabel: It is used to add a label to the Axes’s y-axis.
  5. title: It is used to add title to the axes. 
  6. figtext: It is used to add text at an arbitrary location of the figure.
  7. suptitle: It is used to add title to the figure.

Examples

import matplotlib.pyplot as plt
import numpy as np
f=plt.figure()
ax=f.add_subplot(111)
x=np.array([1,2,3,4])
y=np.exp(x)
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Random Graph')
plt.text(0.5,0.5,'Welcome To Python',horizontalalignment='center',
     verticalalignment='center',transform = ax.transAxes,fontsize=12, color='blue',style='italic')
plt.plot(x,y, marker='o')
plt.show()

Output:

working_with_text

Tue, 11/30/2021 - 08:46

Authored by

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.
Tags