Search code examples
pythonmatplotlibfontsseaborn

Using a custom font in seaborn


I have a custom .ttf file and I am trying to use it in a seaborn plot. Here's what I have so far:

from matplotlib import font_manager
path = "path/to/Roboto-Black.ttf"
fm = font_manager.FontManager()
fm.addfont(path)

prop = font_manager.FontProperties(fname=path)
sns.set(font=prop.get_name())

But this doesn't work, I simply get the warning:

findfont: Font family 'Roboto' not found.

I am using the latest matplotlib version 3.6.1. Also, I cannot install any fonts in the system as I don't have sudo access.


Solution

  • There is a default FontManager instance that needs to be used rather than creating a new instance:

    from matplotlib.font_manager import fontManager, FontProperties
    
    path = "path/to/Roboto-Black.ttf"
    fontManager.addfont(path)
    
    prop = FontProperties(fname=path)
    sns.set(font=prop.get_name())
    

    See source for more details.