Matplotlib font family not found

I’m trying to set the font for plots in matplotlib using rcParams:

mp.rc('font', family = 'sans-serif') # 'Arial' # font group is sans-serif
mp.rcParams["font.sans-serif"] = ["Arial"]

When deployed, I receive the following errors:

findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans.
findfont: Generic family 'sans-serif' not found because none of the following families were found: Arial

I tried runnin st.write(plt.rcParams["font.sans-serif"][:]) , which returns the following list:

"Bitstream Vera Sans",  
 "Computer Modern Sans Serif",   
"Lucida Grande",   
"Verdana",   
"Geneva",   
"Lucid",   
"Arial",   
"Helvetica",  
"Avant Garde",   
"sans-serif"]

How should I get matplotlib to import fonts correctly? I couldn’t find any similar issues.

My requirements.txt:

streamlit==1.35.0
numpy==1.26.4
matplotlib==3.8.0
pandas==2.1.4
numpy==1.26.4
scipy==1.11.4
openpyxl==3.0.10

Hi @S11 . Just use the matplotlib==3.1.1 version.

Happy Streamlit-ing :balloon:

1 Like

Is that the latest version supported by Streamlit for some reason?

Update: this did not change anything, I tried several matplotlib versions with no sucess

Arial is a windows font and it is not installed in strealit cloud. Isn’t the replacement close enough?

1 Like

Thank you, at least I understand why this is an issue now! I tried using apt-get to install ttf-mscorefonts-install, which also could not be found
I’m not sure if other font options are installed onto the streamlit cloud then. As far as I can tell the only font available is the default (DejaVu Sans). For my specific purposes, I could use another sans serif font, but not DejaVu Sans. I guess I could try installing Arial from a .ttf file in the repo itself

Try “Liberation Sans” instead, it is probably installed.

If you insist in Arial, this is how you install apt packages in streamlit cloud:

No luck on Liberation Sans, since mp.rcParams["font.sans-serif"] = "Liberation Sans" returns

findfont: Generic family 'sans-serif' not found because none of the following families were found: Liberation Sans

Also could not install Microsoft fonts, since I get the error

Package ttf-mscorefonts-installer is not available, but is referred to by another package.

This may mean that the package is missing, has been obsoleted, or

is only available from another source

E: Package 'ttf-mscorefonts-installer' has no installation candidate

when I deploy with the following packages.txt:

ttf-mscorefonts-installer

ttf-mscorefonts-installer is in contrib, maybe it is not available in streamlit cloud.

Installing the .ttf file may or may not work, you would have to do it from the application itself, which runs with very few permissions.

Try installing fonts-liberation. This code works for me locally with that package installed (it is Arch but that should not make a difference).

import matplotlib as mp
import matplotlib.pyplot as plt

mp.rcParams["font.sans-serif"] = ["Liberation Sans"]
plt.plot([1, 2, 3])
plt.show()

Thank you so much! This worked great. It was also super helpful that you linked the Debian package list, which I hadn’t seen previously. For anyone else who would like to substitute, there is a helpful list of stable Debian fonts packages. I think all of these should be installable by adding their names to packages.txt. You can then set the matplotlib font as usual,

mp.rcParams["font.sans-serif"] = "Liberation Sans"

The list includes font support for other languages, which for some applications might work and is easier than other solutions I had seen to actually install .ttf files through either mp.font_manager or Path.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.