Created
April 30, 2021 22:36
-
-
Save sukram42/9461c75127a44f996e93d886e7241622 to your computer and use it in GitHub Desktop.
[Python] Add Custom Font to Matplotlib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import Libraries | |
import matplotlib.font_manager as font_manager | |
import urllib.request | |
from tempfile import NamedTemporaryFile | |
def download_font(url: str, ttf_file: str, github: bool=True): | |
# Download the Font | |
font_url = f"{url}{ttf_file}{'?raw=true' if github else ''}" | |
urllib.request.urlretrieve(font_url, ttf_file) | |
# Add the Font to Matplotlib | |
fonts = matplotlib.font_manager.findSystemFonts(fontpaths=".", fontext='ttf') | |
for font_file in fonts: | |
font_manager.fontManager.addfont(font_file) | |
# Download fonts | |
download_font("https://github.com/impallari/Raleway/blob/master/fonts/v3.000%20Fontlab/TTF/", "Raleway-ExtraLight.ttf") | |
# Apply the fonts | |
plt.rcParams['font.family'] = 'serif' | |
plt.rcParams['font.serif'] = 'Raleway' | |
plt.rcParams['size'] = 15 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment