PyFonts: a simple way to load fonts for matplotlib

logo of a chart:pyfonts

PyFonts is a library that allows to load easily any font from the web and use it in your matplotlib charts.

It was created by Joseph Barbier in order to simplify the process to loading fonts in matplotlib and remove the need to install them on your computer.

⏱ Quick start

Before using pyfonts you need to install it. This can easily be done with pip:


pip install pyfonts

pyfonts provides 1 simple function:load_font(): load a font from the web (Github) and return a matplotlib font object.

Basic use case of pyfonts

Basic use case of pyfonts

# Load pyfonts and matplotlib
import matplotlib.pyplot as plt
from pyfonts import load_font

# load font from Github
font = load_font(
   font_url="https://github.com/google/fonts/blob/main/apache/ultra/Ultra-Regular.ttf?raw=true"
)

# Create a plot with an annotation that uses the loaded font
fig, ax = plt.subplots(figsize=(10, 6), dpi=300)
ax.text(
   x=0.5,
   y=0.5,
   s=f"What an easy way to load fonts, isn't it?",
   font=font,
   fontsize=20,
   ha="center",
)
plt.show()

1- How to find fonts

There are many fonts available on the web. The easiest way to find one is to follow these steps:

  • Browse Google Font website to find a font that you like. Let's say you want to use Amaranth.
  • Go to the Google font GitHub repository and type the name of your desired font in the search bar. We find that Amaranth font file in Bold is named https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf.
  • Copy the URL and add ?raw=true at the end, which gives us https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf?raw=true.

2 - Load a font in matplotlib

Use load_font() by passing the font's URL.

Then, provide the output of load_font() directly to any matplotlib function that accepts a font, such as ax.text().

how to load a font with pyfonts

how to load a font with pyfonts

import matplotlib.pyplot as plt
from pyfonts import load_font

font = load_font(
  font_url="https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf?raw=true"
)

fig, ax = plt.subplots(figsize=(10, 10), dpi=300)
ax.text(
    x=0.5,
    y=0.5,
    s=f"Amaranth font\nwith PyFonts!!!",
    font=font,
    fontsize=50,
    ha="center",
)
plt.show()

Different weight and style

When you load a font, you don't load all its extensions: bold, italic, thin etc, but only the one from the url. If you want to be able to use a font and its bold version, for example, you need to load both fonts:

Combine a normal font and a bold font with pyfonts

Combine a normal font and a bold font with pyfonts

import matplotlib.pyplot as plt
from pyfonts import load_font

font = load_font(
   font_url="https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Regular.ttf?raw=true"
)
bold_font = load_font(
   font_url="https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf?raw=true"
)

fig, ax = plt.subplots(figsize=(6, 6), dpi=300)
ax.text(
   x=0.5,
   y=0.5,
   s=f"Congrats, you now have a cool font!",
   font=font,
   fontsize=20,
   ha="center",
)
ax.text(x=0.5, y=0.3, s=f"And now it's bold", font=bold_font, fontsize=25, ha="center")
plt.show()

Locally stored font

PyFonts also allows you to load a font file that you have on your own computer. You just have to call the font_path argument instead and give it the path to your font

Load a locally stored font with pyfonts

Load a locally stored font with pyfonts

import matplotlib.pyplot as plt
from pyfonts import load_font

font = load_font(
   font_path="path/to/myfont/Ultra-Regular.ttf"
)

fig, ax = plt.subplots(figsize=(6, 6), dpi=300)
ax.text(
   x=0.5,
   y=0.5,
   s=f"Yet another way to load font",
   font=font,
   fontsize=18,
   ha="center",
)
plt.show()

Gallery of examples

Here are some examples of what you can do with PyFonts. Click on the images to see the code.

Going further

You might be interested in

Contact


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥