[Python] Matplotlib reports font family 'serif' not found

Discussão em 'Python' iniciado por Stack, Março 22, 2025.

  1. Stack

    Stack Membro Participativo

    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 ')
    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.

    Continue reading...

Compartilhe esta Página