Matplotlib reports font family 'serif' not found

Viewed 8944

When I try and run, for example:

mpl.rcParams['font.family'] = 'serif'
plt.rcParams['figure.figsize'] = [15,7]
plt.plot(data['flow-time'], data['staticpressurerecovery'])
plt.xlabel('Time [s]')
plt.ylabel('Static Pressure Recovery [-]')
plt.title('McD13_4S3 Plenum: Performance Coefficient ')
plt.ylim((0.33, 0.4))
plt.grid()
plt.show()

in a Jupyter notebook, I get the following error message:

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\font_manager.py:1331: UserWarning: findfont: Font family ['serif'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))

Things I've Tried:

  1. Deleting the fontList.cache, fontList.json, and fontList.py3.cache

  2. Uncommenting the font family related sections of the matplotlibrc file

  3. Uninstalled and reinstalled matplotlib using pip uninstall matplotlib and pip install matplotlib

Nothing has solved the issue. The only possible way for me to get different fonts now is to use LaTeX as the backend, but that's slow and unnecessary.

Any ideas what I can try next?

Edit: I'm using Windows 10, so not using apt-get for me. That seems to be a common fix for these problems, but I can't do it. It appears those solutions just add Microsoft fonts to the Linux font manager, so it's probably not even relevant since I'm already on a Microsoft machine.

Minimum working Example:

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

mpl.rcParams['font.family'] = 'serif'
mpl.rcParams['font.serif'] = 'Computer Modern'


t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
plt.plot(t, s)
plt.show()

In fontList.json, Computer Modern is listed as an available font.

1 Answers

I had the same issue. For me, changing my matplotlib version from back to 1.5.3 solves the issue. You need to pip uninstall matplotlib first then pip install matplotlib==1.5.3.

More context, I was using ggplot on zeppelin notebook with Amazon EC2. Somehow the matplotlib version installed was 2.2.3.

After changing the version, the error is gone

Related