Matplotlib 在导入时占用时间

我刚刚升级到 matplotlib(1.5.1)的最新稳定版本,每次导入 matplotlib 都会收到这样的消息:

/usr/local/lib/python2.7/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')

总是会停几秒钟。

这是预期的行为吗? 以前也是这样,只是没有打印出来的信息?

49994 次浏览

As tom suggested in the comment above, deleting the files:

fontList.cache
fontList.py3k.cache
tex.cache

solve the problem. In my case the files were under:

`~/.matplotlib`

EDITED

A couple of days ago the message appeared again, I deleted the files in the locations mention above without any success. I found that as suggested here by T Mudau there's an extra location with text cache files is: ~/.cache/fontconfig

On OSX Yosemite (version 10.10.15), the following worked for me:

  • remove the cache files from this directory as well: ~/.cache/fontconfig (as per tom's suggestion)
    rm -rvf ~/.cache/fontconfig/*
  • also removed .cache files in ~/.matplotlib (as per Hugo's suggestion)
    rm -rvf ~/.matplotlib/*

I ran the python code w. sudo and it cured it...my guess was that there wasn't permission to write that table... good luck!

I ran the python code using sudo just once, and it resolved the warning for me. Now it runs faster. Running without sudo gives no warning at all.

Cheers

This worked for me:

sudo apt-get install libfreetype6-dev libxft-dev

Confirmed Hugo's approach works for Ubuntu 14.04 LTS/matplotlib 1.5.1:

  • deleted ~/.cache/matplotlib/fontList.cache
  • ran code, again the warning was issued (assumption: is rebuilding the cache correctly)
  • ran code again, no more warning (finally)

HI you must find this file : font_manager.py in my case : C:\Users\gustavo\Anaconda3\Lib\site-packages\matplotlib\ font_manager.py

and FIND def win32InstalledFonts(directory=None, fontext='ttf') and replace by :

def win32InstalledFonts(directory=None, fontext='ttf'): """ Search for fonts in the specified font directory, or use the system directories if none given. A list of TrueType font filenames are returned by default, or AFM fonts if fontext == 'afm'. """

from six.moves import winreg
if directory is None:
directory = win32FontDirectory()


fontext = get_fontext_synonyms(fontext)


key, items = None, {}
for fontdir in MSFontDirectories:
try:
local = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, fontdir)
except OSError:
continue


if not local:
return list_fonts(directory, fontext)
try:
for j in range(winreg.QueryInfoKey(local)[1]):
try:
key, direc, any = winreg.EnumValue(local, j)
if not is_string_like(direc):
continue
if not os.path.dirname(direc):
direc = os.path.join(directory, direc)
direc = direc.split('\0', 1)[0]


if os.path.splitext(direc)[1][1:] in fontext:
items[direc] = 1
except EnvironmentError:
continue
except WindowsError:
continue
except MemoryError:
continue
return list(six.iterkeys(items))
finally:
winreg.CloseKey(local)
return None

This worked for me on Ubuntu 16.04 LST with Python 3.5.2 | Anaconda 4.2.0 (64-bit). I deleted all of the files in ~/.cache/matplotlib/.

sudo rm -r fontList.py3k.cache tex.cache

At first I thought it wouldn't work, because I got the warning afterward. But after the cache files were rebuilt the warning went away. So, close your file, and reopen again(open again), it has no warning.