Skip to content

Load font


pyfonts.load_font(font_url=None, use_cache=True, font_path=None)

Loads a matplotlib FontProperties object from a remote url or a local file, that you can then use in your matplotlib charts.

This function is most useful when the foot you are looking for is stored locally or is not available in Google Fonts. Otherwise, it's easier to use the load_google_font() function instead.

If the url points to a font file on Github, add ?raw=true at the end of the url (see examples below).

Parameters
  • font_url: It may be one of the following:

    • A URL pointing to a binary font file.
    • The local file path of the font.
  • use_cache: Whether or not to cache fonts (to make pyfonts faster). Default to True.

  • font_path: (deprecated) The local file path of the font. Use font_url instead.

Returns
  • matplotlib.font_manager.FontProperties: A FontProperties object containing the loaded font.
Usage
from pyfonts import load_font

font = load_font(
    "https://github.com/JosephBARBIERDARNAL/pyfonts/blob/main/tests/Ultra-Regular.ttf?raw=true"
)


Examples

Most font files are stored on Github, but to pass a valid font url, you need to add ?raw=true to the end of it.

So the url goes from:

https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf

To:

https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf?raw=true

What's more, if you find a font on the Google font repo (for example, here: https://github.com/google/fonts/), it will probably be easier to use the load_google_font() function.

Basic usage

# mkdocs: render
import matplotlib.pyplot as plt
from pyfonts import load_font

font = load_font(
   "https://github.com/JosephBARBIERDARNAL/pyfonts/blob/main/tests/Ultra-Regular.ttf?raw=true"
)

fig, ax = plt.subplots()
ax.text(
   x=0.2,
   y=0.3,
   s="Hey there!",
   size=30,
   font=font
)

Custom font

# mkdocs: render
import matplotlib.pyplot as plt
from pyfonts import load_font

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

fig, ax = plt.subplots()
ax.text(
   x=0.2,
   y=0.3,
   s="Hey there!",
   size=30,
   font=font
)

Use multiple fonts

# mkdocs: render
import matplotlib.pyplot as plt
from pyfonts import load_font

font_1 = load_font(
   "https://github.com/JosephBARBIERDARNAL/pyfonts/blob/main/tests/Ultra-Regular.ttf?raw=true"
)
font_2 = load_font(
   "https://github.com/google/fonts/blob/main/ofl/amaranth/Amaranth-Bold.ttf?raw=true"
)

fig, ax = plt.subplots()

ax.text(
   x=0.2,
   y=0.3,
   s="Hey there!",
   size=30,
   font=font_1
)

ax.text(
   x=0.4,
   y=0.6,
   s="Hello world",
   size=30,
   font=font_2
)