28

I'm trying to use the font "Heuristica" in my matplotlib plots, but it won't show up.

I defined "Heuristica" on the first spot in the rcParameter font.serif --> no result

I changed font.family to "Heuristica" and got the message

findfont: FontFamily not found

that got me thinking, because Heuristica is installed and I can access it from other software without problems. So I used the fontManager and did:

import pylab as pl
la = pl.matplotlib.font_manager.FontManager()
lu = pl.matplotlib.font_manager.FontProperties(family = 'Heuristica')
la.findfont(lu)

and got:

Out[7]: 'C:\\Windows\\Fonts\\Heuristica-Regular.otf'

So obviously Heuristica can be found. I looked up the available ttf-Fonts (How can i get list of font family(or Name of Font) in matplotlib) but Heuristica is not in this list.

I'd be glad about any help.

Community
  • 1
  • 1
MichaelA
  • 1,866
  • 2
  • 23
  • 38
  • What version of mpl are you using? – tacaswell Sep 28 '14 at 16:10
  • I am using Version 1.3.1 – MichaelA Sep 28 '14 at 17:16
  • try updating to 1.4.0. If that does not work please make an issue on github – tacaswell Sep 28 '14 at 17:17
  • 2
    The problem persist under 1.4.0. Are you sure that is really an issue for Github and not an issue with the person in front of my PC? Regarding a github issue: May I link to this question or do I a copy & paste? – MichaelA Sep 28 '14 at 18:31
  • Please put enough information is the issue that we don't have to link back to SO. Questions here are not stable (both due to edits and deletions). Please include enough detail that someone with a windows machine can reproduce it (including if you have changed your rcparam file and the _full_ traceback). – tacaswell Sep 28 '14 at 21:36
  • We now have an Issue on Github: [#3590](https://github.com/matplotlib/matplotlib/issues/3590) – MichaelA Sep 29 '14 at 08:33

2 Answers2

53

Well, mdboom solved the problem over at github, all the credit belongs to him:

When you add new fonts to your system, you need to delete your fontList.cache file in order for matplotlib to find them.

The reason it works on lines 4/5 in your example is because you are creating a FontManager from scratch (which goes out to the filesystem and hunts down all the fonts). Internally, when matplotlib later does its own font lookup, it is using a FontManager that has been loaded from a cache on disk in the fontList.cache file.

Long term, we have plans to switch to using the font lookup mechanisms of the OS to get around this problem, (see MEP14), but in the meantime, you'll need to remove the fontList.cache file everytime you want matplotlib to discover new fonts.

The file fontList.cache is located at your Userfolder --> .matplotlib/fontList.cache, for Windows that would normally be C:\Users\yourUsername\.matplotlib\fontList.cache

Community
  • 1
  • 1
MichaelA
  • 1,866
  • 2
  • 23
  • 38
  • 22
    `~/.cache/matplotlib` on Ubuntu. – imrek Aug 22 '15 at 16:55
  • 1
    Nevertheless, it does not work, the font manager is unable to find any reasonable font. – imrek Aug 22 '15 at 16:56
  • 2
    I tried the solution only on windows and can't test on Linux. But maybe you want to add something to the github Issue, which is probably the best way to get a bugfix: https://github.com/matplotlib/matplotlib/issues/3590 – MichaelA Aug 24 '15 at 16:26
  • This came as a suggested edit and I think it is worth mentioning (though I did not check if it is true): If you are using the WinPython distribution you need to also delete the font cache found in e.g.: C:\Python3\settings\.matplotlib It is not sufficient to delete the font cache in your home folder, Matplotlib will still be using the second font cache. – MichaelA Mar 23 '17 at 08:49
  • This hasnt worked for me. Matplotlib version 1.5.1. Is there a new way around this perhaps? – ashley May 25 '17 at 13:05
  • @ashley On Windows you need to look in %HOMEPATH%\.matplotlib. There's a file fontList.py3k.cache. Delete it. If you are using jupyter notebooks you must restart jupyter before the new fonts are picked up and the cache recreated. – Tom Johnson Jun 19 '17 at 19:43
  • 6
    I also had to delete `fontList.json`. – mcmayer Nov 27 '17 at 09:26
  • 6
    Forcing matplotlib to rebuild the cache is probably the most cross-platform solution with `import matplotlib.font_manager as font_manager; font_manager._rebuild()` – raphael Jul 04 '19 at 15:40
  • 3
    For me the file was called `fontlist-v330.json` – SWdV Nov 25 '22 at 13:08
3

For some versions of Matplotlib, it may be necessary to clear the LRU cache of _get_fontconfig_fonts() (next to removing the fontList.cache file).

fm = matplotlib.font_manager
fm._get_fontconfig_fonts.cache_clear()

This function is responsible for calling, and caching, fc-list on a Linux/Unix system. If your font appears in fc-list, and not in Matplotlib's fonts, even after removing the fontList.cache file, this may be the culprit.

user228395
  • 1,156
  • 1
  • 11
  • 25