Load font
pyfonts.load_font(font_url=None, use_cache=True, danger_not_verify_ssl=False, 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 font 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:
| Name | Type | Description | Default |
|---|---|---|---|
font_url
|
Optional[str]
|
It may be one of the following: - A URL pointing to a binary font file. - The local file path of the font. |
None
|
use_cache
|
bool
|
Whether or not to cache fonts (to make pyfonts faster). Default to |
True
|
danger_not_verify_ssl
|
bool
|
Whether or not to to skip SSL certificate on
|
False
|
font_path
|
Optional[str]
|
(deprecated) The local file path of the font. Use |
None
|
Returns:
| Type | Description |
|---|---|
FontProperties
|
matplotlib.font_manager.FontProperties: A |
Examples:
```python
from pyfonts import load_font
font = load_font(
"https://github.com/y-sunflower/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:
To:
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/y-sunflower/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/y-sunflower/pyfonts/blob/main/tests/Amarante-Regular.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/y-sunflower/pyfonts/blob/main/tests/Ultra-Regular.ttf?raw=true"
)
font_2 = load_font(
"https://github.com/y-sunflower/pyfonts/blob/main/tests/Amarante-Regular.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
)