Skip to content

Instantly share code, notes, and snippets.

@sukram42
Created April 30, 2021 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sukram42/9461c75127a44f996e93d886e7241622 to your computer and use it in GitHub Desktop.
Save sukram42/9461c75127a44f996e93d886e7241622 to your computer and use it in GitHub Desktop.
[Python] Add Custom Font to Matplotlib
# 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