1
votes

I am trying to set the font of a matplotlib plot to Times New Roman. I have tried:

import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'Times New Roman'

I believe this is the correct way to set the font, but I keep getting this error that the found is not found:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/font_manager.py:1238: UserWarning: findfont: Font family ['Times New Roman'] not found. Falling back to DejaVu Sans.

After some searching, I verified that I have the font downloaded. I tried opening a python shell and checking the contents of rcParams myself. Among a bunch of other font parameters, I curiously got a list of fonts in font.serif that contains Times New Roman.

          'font.serif': ['DejaVu Serif',
                         'Bitstream Vera Serif',
                         'Computer Modern Roman',
                         'New Century Schoolbook',
                         'Century Schoolbook L',
                         'Utopia',
                         'ITC Bookman',
                         'Bookman',
                         'Nimbus Roman No9 L',
                         'Times New Roman',
                         'Times',
                         'Palatino',
                         'Charter',
                         'serif'],

However, font.family only contained one item: sans-serif, when the matplotlib documentation states that there should be five values inside font.family. Has anybody run into this error before? How did you fix it?

1
I think on macs, there is no "Times New Roman", only "Times" by default. Maybe the installation has gone wrong? The font.family being set to sans-serif is correct (as is also seen from the link you share). It can be one of five different values.ImportanceOfBeingErnest

1 Answers

1
votes

However, font.family only contained one item: sans-serif, when the matplotlib documentation states that there should be five values inside font.family

No, font.family should contain one of those five values, namely the font family that you want to use.

You gotta make sure that the font family is set to serif and the font Times New Roman is at the top of the serif font list. It works like this:

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

You technically don't need to append the rest of the list (the very last part). It simply provides a fallback for matplotlib if Times New Roman is actually not available.

Also see the example in the docs.