Skip to content

Load Google font


pyfonts.load_google_font(family, weight=None, italic=None, allowed_formats=['woff2', 'woff', 'ttf', 'otf'], use_cache=True)

Load a font from Google Fonts with specified styling options and return a font property object that you can then use in your matplotlib charts.

The easiest way to find the font you want is to browse Google font and then pass the font name to the family argument.

Parameters
  • family: Font family name (e.g., "Open Sans", "Roboto", etc).

  • weight: Desired font weight (e.g., 400, 700) or one of: 'thin', 'extra-light', 'light', 'regular', 'medium', 'semi-bold', 'bold', 'extra-bold', 'black'. Default is None.

  • italic: Whether to use the italic variant. Default is None.

  • allowed_formats: List of acceptable font file formats. Defaults to ["woff2", "woff", "ttf", "otf"].

  • use_cache: Whether or not to cache fonts (to make pyfonts faster). Default to True.

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

font = load_google_font("Roboto") # default Roboto font
font = load_google_font("Roboto", weight="bold") # bold font
font = load_google_font("Roboto", italic=True) # italic font
font = load_google_font("Roboto", weight="bold", italic=True) # italic and bold


Examples

Basic usage

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

font = load_google_font("Roboto") # default Roboto font

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_google_font

font = load_google_font("Roboto", weight="bold", italic=True) # italic and bold

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_google_font

font_bold = load_google_font("Roboto", weight="bold")
font_italic = load_google_font("Roboto", italic=True)

fig, ax = plt.subplots()

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

ax.text(
   x=0.4,
   y=0.6,
   s="Hey italic!",
   size=30,
   font=font_italic
)

Fancy font

All fonts from Google font can be used:

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

font = load_google_font("Barrio")

fig, ax = plt.subplots()
ax.text(
   x=0.1,
   y=0.3,
   s="What a weird font!",
   size=30,
   font=font
)