Skip to content

Load Google font


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

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:

Name Type Description Default
family str

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.

required
italic Optional[bool]

Whether to use the italic variant. Default is None.

None
allowed_formats List[str]

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

['woff2', 'woff', 'ttf', 'otf']
use_cache bool

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

True
danger_not_verify_ssl bool

Whether or not to to skip SSL certificate on ssl.SSLCertVerificationError. If True, it's a security risk (such as data breaches or man-in-the-middle attacks), but can be convenient in some cases, like local development when behind a firewall.

False

Returns:

Type Description
FontProperties

matplotlib.font_manager.FontProperties: A FontProperties object containing the loaded font.

Examples:

```python
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
)