问题: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.')

…总是停顿几秒钟。

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

I just upgraded to the latest stable release of matplotlib (1.5.1) and everytime I import matplotlib I get this message:

/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.')

… which always stalls for a few seconds.

Is this the expected behaviour? Was it the same also before, but just without the printed message?


回答 0

正如汤姆在上面的评论中建议的,删除文件:

fontList.cache
fontList.py3k.cache 
tex.cache 

解决这个问题。就我而言,文件位于:

`~/.matplotlib`

已编辑

几天前,该消息再次出现,我删除了上述位置中的文件,但没有成功。我发现,建议在这里通过牛逼穆道有一个额外的位置信息与文本缓存文件是:~/.cache/fontconfig

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


回答 1

确认的Hugo的方法适用于Ubuntu 14.04 LTS / matplotlib 1.5.1:

  • 删除〜/ .cache / matplotlib / fontList.cache
  • 运行代码,再次发出警告(假设:正在正确地重建缓存)
  • 再次运行代码,不再发出警告(最终)

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)

回答 2

在OSX Yosemite(版本10.10.15)上,以下代码对我有用:

  • 也从该目录中删除缓存文件:〜/ .cache / fontconfig(根据tom的建议)
    rm -rvf ~/.cache/fontconfig/*
  • 还删除了〜/ .matplotlib中的.cache文件(根据Hugo的建议)
    rm -rvf ~/.matplotlib/*

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/*

回答 3

我只使用sudo运行了python代码一次,它为我解决了警告。现在它运行得更快。不使用sudo运行就不会发出任何警告。

干杯

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


回答 4

我运行了python代码w。sudo并治愈了它。。。我猜是没有写这张桌子的许可了……祝你好运!

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!


回答 5

嗨,您必须在我的情况下找到此文件:font_manager.py:C:\ Users \ gustavo \ Anaconda3 \ Lib \ site-packages \ matplotlib \ font_manager.py

和查找def win32InstalledFonts(directory = None,fontext =’ttf’)并替换为:

def win32InstalledFonts(directory = None,fontext =’ttf’):“”“在指定的字体目录中搜索字体,如果未提供,则使用系统目录。默认情况下,返回TrueType字体文件名列表;如果返回,则返回AFM字体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

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

回答 6

这在使用Python 3.5.2的 Ubuntu 16.04 LST上对我有用。Anaconda 4.2.0(64位)。我删除了中的所有文件~/.cache/matplotlib/

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

起初我以为那是行不通的,因为后来我得到了警告。但是在重建缓存文件后,警告消失了。因此,关闭文件,然后重新打开(再次打开),它没有警告。

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.


回答 7

这对我有用:

sudo apt-get install libfreetype6-dev libxft-dev

This worked for me:

sudo apt-get install libfreetype6-dev libxft-dev

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。