J.Doe
J.Doe

Reputation: 434

How do I use any of my system fonts in matplotlib GLOBALLY in the most straight-forward way possible?

First things first: Windows 10, python 3.8, matplotlib 3. (I've had the same problem with Anaconda python 3.7)

I've installed Helvetica from this source in my C:\Windows\Fonts folder. I've tried running this script and get the following error:

import matplotlib.pyplot as plt
from matplotlib import rcParams

rcParams['font.family'] = ['Helvetica Normal']

fig,ax = plt.subplots()

plt.show()
findfont: Font family ['Helvetica Normal'] not found. Falling back to DejaVu Sans.

I've tried Helvetica and Helvetica Standard before that. It works with Trebuchet, which, as far as I know, is only installed in my system fonts and not any of the matplotlib font folders.

I tried a lot of solutions, but I just don't know how to specify the font for matplotlib globally. I've tried every solution I found on github, stackoverflow etc. I've had this problem for over a year now but I can't find a suitable solution to this.

Upvotes: 2

Views: 3801

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339290

  1. Install the font in the system.
  2. Locate the font file (in this case C:\Windows\Fonts\Helvetica 400.ttf)
  3. Run

    fp = matplotlib.font_manager.FontProperties(fname=r"C:\Windows\Fonts\Helvetica 400.ttf")
    print(fp.get_name())
    

    to get the name of the font printed. It would be Helvetica in this case.

  4. Rebuild the font cache. This could be done by deleting the fontlist-vXXX.json (where XXX = 310 for version 3.1.0) file from the matplotlib folder. Or by running matplotlib.font_manager._rebuild() via code.

  5. Use the font name from step 3. in your code, e.g. plt.rcParams['font.family'] = ['Helvetica']

Upvotes: 3

Related Questions