Fixing the Matplotlib basic fonts problem – Matplotlib cannot find basic fonts

Matplotlib basic fonts problem – Matplotlib cannot find basic fonts is an error which usually occurs when you have a problem with matplotlib’s fonts cache.

Today I try to explain why this error takes place and how to solve it, I will also add other solutions that could solve the error if possible.

Exploring the Error : Matplotlib basic fonts problem – Matplotlib cannot find basic fonts

This is an error which usually occurs when you have a problem with matplotlib’s fonts cache.

Please double check so you can avoid mixing between errors. The error message should look like the error message bellow.

                                                                       #
packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family
['sans-serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))
                                                                       #

Bellow we will take care of the error using multiple possible solutions according to your needs.

Solution 1 : clear matplotlib’s fonts cache and move your chosen fonts

The first solution is only for windows users. First, navigate to matplotlib’s fonts Cache location.

You can get the location of the cache folder by printing mpl.font_manager.get_cachedir() like in the example bellow.

                                                                       #
import matplotlib as mpl; 
print(mpl.font_manager.get_cachedir())
                                                                       #

Then Delete the fonts inside that folder.

Finally, you should look for a folder called ttf inside \site-packages\matplotlib\mpl-data\fonts , this is not the full path, you should find this folder’s path depending on your machine.

Once you find the directory copy the fonts you want to use and paste them inside the directory and the error should be gone after the next run.

Try the second and last solution bellow, if that is not the case.

Solution 2 : Correctly install msttcorefonts on Linux

The solution above was for windows users, this solution is for Linux users.

First, you should install msttcorefonts, use root privileges to do that. You can use the command bellow.

                                                                       #
sudo apt install msttcorefonts -qq
                                                                       #

You can also choose this command, try them both and see if the error disappears,

                                                                       #
sudo apt-get install msttcorefonts -qq
                                                                       #

But do not test unless you have cleared matplotlib’s cache, you can do that with the following command.

                                                                       #
rm ~/.cache/matplotlib -rf
                                                                       #

I hope this solution solved your problem.

This error could be confusing at first. But once you understand why it is happening, it is easy to solve by only using a more recent method that works and achieves the same function.

Summing-up

The solutions above should be enough to solve your problem, I hope the article helped you get rid of the issue, please keep learning, keep coding and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/