Python matplotlib 导入时需要时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34771191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:30:06  来源:igfitidea点击:

matplotlib taking time when being imported

pythonmatplotlib

提问by Ricky Robinson

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

我刚刚升级到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.')

... which always stalls for a few seconds.

...它总是停滞几秒钟。

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

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

采纳答案by Hugo

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 hereby T Mudauthere's an extra location with text cache files is: ~/.cache/fontconfig

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

回答by robbbyr

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

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

  • 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/*
  • 也从此目录中删除缓存文件:~/.cache/fontconfig(根据 tom 的建议)
    rm -rvf ~/.cache/fontconfig/*
  • 还删除了 ~/.matplotlib 中的 .cache 文件(根据 Hugo 的建议)
    rm -rvf ~/.matplotlib/*

回答by C a t

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!

我运行了 python 代码 w。sudo 并治愈了它...我的猜测是没有写入该表的权限...祝你好运!

回答by Awais

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.

我只使用 sudo 运行了一次 python 代码,它为我解决了警告。现在它运行得更快。在没有 sudo 的情况下运行根本不会发出警告。

Cheers

干杯

回答by mykahveli

This worked for me:

这对我有用:

sudo apt-get install libfreetype6-dev libxft-dev

回答by Bill Gale

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

确认 Hugo 的方法适用于 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)
  • 删除 ~/.cache/matplotlib/fontList.cache
  • 运行代码,再次发出警告(假设:正在正确重建缓存)
  • 再次运行代码,不再发出警告(终于)

回答by Gus Ch

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

嗨,你必须找到这个文件: font_manager.py 在我的情况下: 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') 并替换为:

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'. """

def win32InstalledFonts(directory=None, fontext='ttf'): """ 在指定的字体目录中搜索字体,如果没有,则使用系统目录。默认返回 TrueType 字体文件名列表,如果是 AFM 字体fonttext== '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('
sudo rm -r fontList.py3k.cache tex.cache 
', 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

回答by S. Theon

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

这在 Ubuntu 16.04 LSTPython 3.5.2上对我有用| 蟒蛇 4.2.0(64 位)。我删除了~/.cache/matplotlib/.

##代码##

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.

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