How to load a .ttf file in Matplotlib using mpl.rcParams?


To load a .ttf file in Matplotlib using mpl.rcParams, we can take the following steps −

  • Set the figure size and adjust the padding between and around the subplots.
  • Initialize the path for the .ttf file.
  • Get an instance of a class for storing and manipulating the font properties.
  • Set the font family with the name of the font that best matches the font properties.
  • Create a figure and a set of subplots.
  • Set the title of the figure.
  • To display the figure, use show() method.

Example

from matplotlib import pyplot as plt, font_manager as fm

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

path = '/usr/share/fonts/truetype/malayalam/Karumbi.ttf'

fprop = fm.FontProperties(fname=path)

plt.rcParams['font.family'] = fprop.get_name()

fig, ax = plt.subplots()
ax.set_title('This is my title!', size=40)

plt.show()

Output

Updated on: 17-Jun-2021

313 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements