-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.py
26 lines (21 loc) · 900 Bytes
/
utility.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import os
import matplotlib as mpl
import matplotlib.font_manager as font_manager
from pathlib import Path
def load_matplotlib_local_fonts(font_path: str, font_size: int) -> None:
"""Load a local font file and update the Matplotlib font configuration"""
# Construct a Path object from the provided font_path
font_path_obj = Path(os.path.join(os.path.dirname(__file__), font_path))
# Check if the file exists
assert font_path_obj.exists(), "Font file does not exist"
# Add the font to Matplotlib's font manager
font_manager.fontManager.addfont(str(font_path_obj))
prop = font_manager.FontProperties(fname=font_path_obj) # type: ignore
# Update the default font family and font size
mpl.rc("font", family="sans-serif")
mpl.rcParams.update(
{
"font.size": font_size,
"font.sans-serif": [prop.get_name()],
}
)