Changing fonts in matplotlib

14,023

Solution 1

In order to see which fonts are available, you should use this method:

import  matplotlib.font_manager
flist = matplotlib.font_manager.get_fontconfig_fonts()
names = [matplotlib.font_manager.FontProperties(fname=fname).get_name() for fname in flist]
print names

Then see if "Times New Roman" is there or not.

if "Times New Roman" in names:
    print "Yes"
else:
    print "font not available"

If it isn't, you can't use it. If it is, the following will work as expected.

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

plt.plot([1,2,3])

plt.title('Please be Times New Roman')

plt.show()

Note that you may specify several fonts. The first that actually is available will be chosen. Adding "serif" last in the list at least makes sure to fall back to a serif font if none of the others is present.

plt.rcParams["font.family"] = "Gulasch", "Times", "Times New Roman", "serif"

Solution 2

I'd the same problem with the same warning:

UserWarning: findfont: Font family ['Times New Roman'] not found. Falling back to DejaVu Sans

I read lots of Stack Overflow posts to no avail until I found this.

Now I can use Times New Roman in matplotlib

In my case I had to use sudo and since Im not using a virtual environment I'd to do this instead:

sudo cp /usr/share/fonts/truetype/msttcorefonts/Times_New_Roman* /usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/fonts/ttf

Another thing that I'd to look up was the last suggestion of the post, i.e.,

Note: If the font is not rendered, it may be because matplotlib needs to refresh its font cache (see Kevin’s comment):

import matplotlib

matplotlib.font_manager._rebuild()

It didn't work for me. Instead I used:

import matplotlib.font_manager as fm

fm._rebuild()

Share:
14,023
A.Aussage
Author by

A.Aussage

Updated on June 04, 2022

Comments

  • A.Aussage
    A.Aussage almost 2 years

    So I've tried pretty much everything I could find on stackoverflow (and anywhere else google would lead me) ; and I just can't change the god damn font !

    Here comes the non-exhaustive list of what I've tried so far :

    Trying as suggested in this question :

    import matplotlib.pyplot as plt
    csfont = {'fontname':'Times New Roman'}
    x = [1,2,3]
    y = x
    
    plt.plot(x,y)
    
    plt.title('Please be Times >__<',**csfont)
    
    plt.show()
    

    gives me this error log :

    >>> (executing file "<tmp 1>")
    Note on using QApplication.exec_():
    The GUI event loop is already running in the pyzo kernel, and exec_()
    does not block. In most cases your app should run fine without the need
    for modifications. For clarity, this is what the pyzo kernel does:
    - Prevent deletion of objects in the local scope of functions leading to exec_()
    - Prevent system exit right after the exec_() call
    /home/antoine/miniconda3/lib/python3.6/site-packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family ['Times New Roman'] not found. Falling back to DejaVu Sans
      (prop.get_family(), self.defaultFamily[fontext]))
    
    >>> 
    

    This answer didn't quite help me either :

    import matplotlib.pyplot as plt
    plt.rcParams["font.family"] = "Times New Roman"
    x = [1,2,3]
    y = x
    
    plt.plot(x,y)
    
    plt.title('Please be Times >__<',**csfont)
    
    plt.show()
    

    There is no error shown ; but this is not Times ...

    Still not Times

    It's also a bit wierd to me that the first attempt gives that error log, as doing what this answer suggests shows that Times New Roman is indeed a font I should be able to use :

    >>> set([f.name for f in matplotlib.font_manager.fontManager.afmlist])
    {'Courier', 'Times', 'URW Bookman L', 'Nimbus Mono L', 'ITC Bookman', 'ZapfDingbats', 'Century Schoolbook L', 'New Century Schoolbook', 'Helvetica', 'Standard Symbols L', 'Utopia', 'Palatino', 'URW Gothic L', 'Courier 10 Pitch', 'Symbol', 'Computer Modern', 'Bitstream Charter', 'ITC Avant Garde Gothic', 'Nimbus Roman No9 L', 'ITC Zapf Chancery', 'ITC Zapf Dingbats', 'URW Chancery L', 'Nimbus Sans L', 'Dingbats', 'URW Palladio L'}
    

    So ... What else can I try ?

  • A.Aussage
    A.Aussage over 6 years
    It appears that 'Times new Roman' is not there ; do I need to forcefully add it as suggested in this question or is there an easier way to go ?
  • ImportanceOfBeingErnest
    ImportanceOfBeingErnest over 6 years
    The question you link to is good. You need to check if there actually is a Times New Roman font file present somewhere, if not add it to the directory, empty the font cache. Could work.
  • A.Aussage
    A.Aussage over 6 years
    Well, it appears that there is a STIX font which looks very similar to times ; I guess it's safer to just use something that's already included !