Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Use Sinhala language fonts for Matplotlib with Google Noto Fonts

Isurie K. Liyanage
Nerd For Tech
Published in
2 min readAug 14, 2021

--

Matplotlib is a cross-platform, graph plotting library in python that serves as a visualization utility creating static, animated, and interactive visualizations. It is used along with NumPy to provide an environment that is an effective open-source alternative for MatLab. It can also be used with graphics toolkits like PyQt and wxPython. This article is about how to install and use Sinhala language fonts in Matplotlib using Google Noto Fonts.

Google Noto Fonts

Google has been developing a font family called Noto, which aims to support all languages with a harmonious look and feel. Noto is a typeface with a large family of fonts, offered for free under the SIL Open Source License by Google. Here we are going to use, NotoSansSinhala a Sinhala Google Noto Font in Google Colaboratory Matplotlib chart by using a custom font from a ttf file.

Use a custom font from a TTF file

Using custom fonts in Matplotlib locally involves storing the .ttfs in the matplotlib/mpl-data/fonts/ttf/ folder. To do this in Google Colaboratory for Sinhala fonts, here is what you need to do!

Here I’ve worked with Python version: 3.7.11
matplotlib: 3.2.2

We need to install our custom font. And Matplotlib expects a font in True Type format (.ttf). For example, if we want to add the Sinhala font, we need to check if we have the font in ttf format installed on our system otherwise we need to download it and install it. Noto Sans Sinhala font can be from here.

Download the font package (.zip):

!wget "https://noto-website-2.storage.googleapis.com/pkgs/NotoSansSinhala-hinted.zip"

Uncompress the package:

!unzip "NotoSansSinhala-hinted.zip"

Copy the path to the folder:

!mv NotoSansSinhala-Medium.ttf /usr/share/fonts/truetype/
Plotting a graph with title, y label, and x label in Sinhala font.

Now we need to instruct Matplotlib to use our custom font. We can use Matplotlib font_manager to add fonts from a file.

In case we want to plot a bar chart and change the labels and title in Sinhala font, we will use the Sinhala Sans font.

The following code snippet in the notebook shows you how to use Sinhala Sans font in the labels and the title:

Summary

  1. Download fonts of choice. Here we download Sinhala Sans font to the current directory and it is not necessary to download to the share or matplotlib folders: /usr/share/fonts/truetype
  2. Pick up the font in the current directory: If you do end up downloading the fonts to /usr/share/fonts/truetyp , change this to: fm.FontProperties()
  3. Use new font on all your plots using FontProperties().

--

--

Nerd For Tech
Nerd For Tech

Published in Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Isurie K. Liyanage
Isurie K. Liyanage

Written by Isurie K. Liyanage

Technical Writer | BSc. (Hons.) IT — UoM | Software Engineer

No responses yet