60
votes

I am using matplotlib version 2.0.0 on Python 3 in a miniconda virtual environment. I am working on a unix scientific computing cluster where I don't have root privileges. I am generally executing python code through an ipython notebook. If I do a basic command such as:

import matplotlib.pyplot as plt
plt.scatter([1,5], [1,5])

I get an error message:

path_to_miniconda/miniconda3/envs/conda34/lib/python3.4/site-
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]))

I would like to be able to use a Times New Roman font but even after deleting the Font cache file (fontList.py3k.cache) which I find from here:

import matplotlib as mpl
fm = mpl.font_manager
fm.get_cachedir()

The commands:

mpl.rcParams['font.family'] = ['serif']
mpl.rcParams['font.serif'] = ['Times New Roman']

have no effect, I get the same error as above. The true type fonts directory:

path_to_miniconda/miniconda3/envs/conda34/lib/python3.4/site-packages/matplotlib/mpl-data/fonts/ttf/

only has 40 fonts in it of the type: DejaVuSerif,DejaVuSans,STIX,cmb, cmt, cmy

Any idea what could be going on and how I can add additional fonts? Thanks!

12
I had the same problem. Thank you for your tip fm.get_cachedir(), I was erasing the wrong cache and now it works! Thank you :)2D_
I have the same error, but my figures end up in serif (using latex).Sören
Try reinstall, matplotlib released version 3 in 2018user3226167

12 Answers

37
votes

To get it to work, I had to combine the two current top answers. Here's what worked for me:

$ sudo apt install msttcorefonts -qq
$ rm ~/.cache/matplotlib -rf
30
votes

I had this exact same problem on a Vagrant VM running Ubuntu Xenial 64-bit. No matter how many fonts I had already installed, matplotlib was having a problem with the "system" font name "sans-serif". I fixed it by:

  • Stopping Jupyter
  • Installing font-manager: sudo apt install font-manager
  • Cleaning the matplotlib cache directory: rm ~/.cache/matplotlib -fr
  • Restarting Jupyter.

No more error messages about sans-serif.

25
votes

This work for me::

$ sudo apt-get install msttcorefonts -qq
13
votes

A solution for Windows users, when confronted with the warning:

UserWarning: findfont: Font family ['serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))
  1. Delete the fonts located in matplotlib's cache.
    Cache's location: import matplotlib as mpl; print(mpl.font_manager.get_cachedir())

  2. Find matplotlib's font directory. The path might be similar to
    C:\Miniconda3\pkgs\matplotlib-2.2.2-py36_1\Lib\site-packages\matplotlib\mpl-data\fonts\ttf

  3. Copy necessary fonts like Computer Modern to this directory.

The warning may persist, but your plots' font should change appropriately.

5
votes

I actually solved by

import matplotlib.font_manager

3
votes

It took me many hours to finally figure out that I needed to update matplotlib.

My original version of 3.0.3 (which came with my jupyter-datascience docker image) would give me this error, but updating to 3.1.1 fixed it.

In the end, my docker script:

RUN pip install matplotlib==3.1.1
COPY fonts /usr/share/fonts/truetype/
RUN fc-cache -fv
RUN rm /home/jovyan/.cache/matplotlib -rf

is what did it for me.

2
votes

There is a conda package for it[1]. So, you don't really need sudo to fix this!

conda install -c conda-forge -y mscorefonts

restart jupyter and/or force rebuild matplotlib font cache

import matplotlib
matplotlib.font_manager._rebuild()

[1] - https://anaconda.org/conda-forge/mscorefonts

2
votes

I had this problem with anaconda env (it could be useful in other related situations as well). For example, for base env, I have checked the following directory: /home/***/anaconda3/pkgs/matplotlib-base-3.1.3-py37hef1b27d_0/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf/

and noticed that I have DejaVuSerif.ttf intalled there, so instead of using plt.rcParams['font.family'] = 'Serif' I used plt.rcParams['font.family'] = 'DeJavu Serif' ( there is a space between "DeJavu and serif" ). i worked for me.

import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'DeJavu Serif'
plt.rcParams['font.serif'] = ['Times New Roman']
1
votes

I was facing a similar issue in a Cloud Datalab docker image running on a gcloud VM. Executing the following fixed the issue for me:

$ sudo apt install msttcorefonts -qq
$ rm ~/.cache/matplotlib -rf

Here is instructions on how to get to the docker image running on the gcloud VM containing the Datalab instance just in case.

1
votes

try all the methods above, not work for me.

my way to solve this is a little dirrent cause I'm using conda on ubuntu server running jupyter

locate -b '\mpl-data'

find a folder

/usr/share/matplotlib/mpl-data

then I add the simhei fonts into the font folder in mpl-data . then remove matplotlib fonts cache

rm -rf ~/.cache/matplotlib

restart jupyter notebook, and it works.

0
votes

I am in macOS with jupyter notebook, I solved with the following, first close your jupyter notebook. Then find out the font path by doing the following in Python

import matplotlib
print(matplotlib.matplotlib_fname())

it prints /Users/zyy/anaconda2/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc for me, notice matplotlibrc is a file, not a directory.

Then download font SimHei, and copy it to the directory fonts/ttf under the mpl-data/ directory above.

Delete directory ~/.cache/matplotlib and restart your jupyter notebook, everything should be good.

0
votes

For windows users

  1. Just go to the cache dir of matplotlib by using

import matplotlib as mpl

print(mpl.font_manager.get_cachedir())

  1. Clear the entire cache file

  2. Lastly again import matplotlib

Hopefully u will also find it helpful as it worked for me

P:S - If you wish you can restart your jupyter/ide (optional) after this