在FT2Font中:无法加载字体

8

我正在尝试绘制一张条形图。但是它显示运行时错误,并显示在底部的FT2Font:无法加载面部。 我尝试过卸载和重新安装它。并尝试了另一种方法,但仍然无法工作。我认为代码是正确的,因为我成功地在Windows笔记本电脑上运行它。我的jupyter笔记本是用于Mac Python 2.7。

以下是代码:

import numpy as np
import pandas as pd
obj = pd.Series([4, 7, -5, 3])
print type(obj)
obj
%matplotlib inline
obj.plot(kind = 'bar', figsize=[5,4])

但是它显示了一些错误:
<matplotlib.axes._subplots.AxesSubplot at 0x1114e69d0>
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/anaconda2/lib/python2.7/site-packages/IPython/core/formatters.pyc in __call__(self, obj)
    332                 pass
    333             else:
--> 334                 return printer(obj)
    335             # Finally look for special method names
    336             method = get_real_method(obj, self.print_method)

/anaconda2/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in <lambda>(fig)
    238 
    239     if 'png' in formats:
--> 240         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    241     if 'retina' in formats or 'png2x' in formats:
    242         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

/anaconda2/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in print_figure(fig, fmt, bbox_inches, **kwargs)
    122 
    123     bytes_io = BytesIO()
--> 124     fig.canvas.print_figure(bytes_io, **kw)
    125     data = bytes_io.getvalue()
    126     if fmt == 'svg':

/anaconda2/lib/python2.7/site-packages/matplotlib/backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2206                     orientation=orientation,
   2207                     dryrun=True,
-> 2208                     **kwargs)
   2209                 renderer = self.figure._cachedRenderer
   2210                 bbox_inches = self.figure.get_tightbbox(renderer)

/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in print_png(self, filename_or_obj, *args, **kwargs)
    505 
    506     def print_png(self, filename_or_obj, *args, **kwargs):
--> 507         FigureCanvasAgg.draw(self)
    508         renderer = self.get_renderer()
    509         original_dpi = renderer.dpi

/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in draw(self)
    428             if toolbar:
    429                 toolbar.set_cursor(cursors.WAIT)
--> 430             self.figure.draw(self.renderer)
    431         finally:
    432             if toolbar:

/anaconda2/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args, **kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

/anaconda2/lib/python2.7/site-packages/matplotlib/figure.pyc in draw(self, renderer)
   1293 
   1294             mimage._draw_list_compositing_images(
-> 1295                 renderer, self, artists, self.suppressComposite)
   1296 
   1297             renderer.close_group('figure')

/anaconda2/lib/python2.7/site-packages/matplotlib/image.pyc in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    136     if not_composite or not has_images:
    137         for a in artists:
--> 138             a.draw(renderer)
    139     else:
    140         # Composite any adjacent images together

/anaconda2/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args, **kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_base.pyc in draw(self, renderer, inframe)
   2397             renderer.stop_rasterizing()
   2398 
-> 2399         mimage._draw_list_compositing_images(renderer, self, artists)
   2400 
   2401         renderer.close_group('axes')

/anaconda2/lib/python2.7/site-packages/matplotlib/image.pyc in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    136     if not_composite or not has_images:
    137         for a in artists:
--> 138             a.draw(renderer)
    139     else:
    140         # Composite any adjacent images together

/anaconda2/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args, **kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

/anaconda2/lib/python2.7/site-packages/matplotlib/axis.pyc in draw(self, renderer, *args, **kwargs)
   1133         ticks_to_draw = self._update_ticks(renderer)
   1134         ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
-> 1135                                                                 renderer)
   1136 
   1137         for tick in ticks_to_draw:

/anaconda2/lib/python2.7/site-packages/matplotlib/axis.pyc in _get_tick_bboxes(self, ticks, renderer)
   1073         for tick in ticks:
   1074             if tick.label1On and tick.label1.get_visible():
-> 1075                 extent = tick.label1.get_window_extent(renderer)
   1076                 ticklabelBoxes.append(extent)
   1077             if tick.label2On and tick.label2.get_visible():

/anaconda2/lib/python2.7/site-packages/matplotlib/text.pyc in get_window_extent(self, renderer, dpi)
    968             raise RuntimeError('Cannot get window extent w/o renderer')
    969 
--> 970         bbox, info, descent = self._get_layout(self._renderer)
    971         x, y = self.get_unitless_position()
    972         x, y = self.get_transform().transform_point((x, y))

/anaconda2/lib/python2.7/site-packages/matplotlib/text.pyc in _get_layout(self, renderer)
    352         tmp, lp_h, lp_bl = renderer.get_text_width_height_descent('lp',
    353                                                          self._fontproperties,
--> 354                                                          ismath=False)
    355         offsety = (lp_h - lp_bl) * self._linespacing
    356 

/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in get_text_width_height_descent(self, s, prop, ismath)
    233 
    234         flags = get_hinting_flag()
--> 235         font = self._get_agg_font(prop)
    236         font.set_text(s, 0.0, flags=flags)  # the width and height of unrotated string
    237         w, h = font.get_width_height()

/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in _get_agg_font(self, prop)
    270         font = get_font(
    271             fname,
--> 272             hinting_factor=rcParams['text.hinting_factor'])
    273 
    274         font.clear()

/anaconda2/lib/python2.7/site-packages/backports/functools_lru_cache.pyc in wrapper(*args, **kwds)
    135                         stats[HITS] += 1
    136                         return result
--> 137                 result = user_function(*args, **kwds)
    138                 with lock:
    139                     root, = nonlocal_root

RuntimeError: In FT2Font: Can not load face.

<matplotlib.figure.Figure at 0x1114e6290>

我已经尝试卸载它。还有另一段代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
obj = pd.Series([4, 7, -5, 3])
print type(obj)
obj
plt.plot(obj)
plt.show()

仍然显示错误:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/anaconda2/lib/python2.7/site-packages/IPython/core/formatters.pyc in __call__(self, obj)
    332                 pass
    333             else:
--> 334                 return printer(obj)
    335             # Finally look for special method names
    336             method = get_real_method(obj, self.print_method)

/anaconda2/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in <lambda>(fig)
    238 
    239     if 'png' in formats:
--> 240         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    241     if 'retina' in formats or 'png2x' in formats:
    242         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

/anaconda2/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in print_figure(fig, fmt, bbox_inches, **kwargs)
    122 
    123     bytes_io = BytesIO()
--> 124     fig.canvas.print_figure(bytes_io, **kw)
    125     data = bytes_io.getvalue()
    126     if fmt == 'svg':

/anaconda2/lib/python2.7/site-packages/matplotlib/backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2206                     orientation=orientation,
   2207                     dryrun=True,
-> 2208                     **kwargs)
   2209                 renderer = self.figure._cachedRenderer
   2210                 bbox_inches = self.figure.get_tightbbox(renderer)

/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in print_png(self, filename_or_obj, *args, **kwargs)
    505 
    506     def print_png(self, filename_or_obj, *args, **kwargs):
--> 507         FigureCanvasAgg.draw(self)
    508         renderer = self.get_renderer()
    509         original_dpi = renderer.dpi

/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in draw(self)
    428             if toolbar:
    429                 toolbar.set_cursor(cursors.WAIT)
--> 430             self.figure.draw(self.renderer)
    431         finally:
    432             if toolbar:

/anaconda2/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args, **kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

/anaconda2/lib/python2.7/site-packages/matplotlib/figure.pyc in draw(self, renderer)
   1293 
   1294             mimage._draw_list_compositing_images(
-> 1295                 renderer, self, artists, self.suppressComposite)
   1296 
   1297             renderer.close_group('figure')

/anaconda2/lib/python2.7/site-packages/matplotlib/image.pyc in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    136     if not_composite or not has_images:
    137         for a in artists:
--> 138             a.draw(renderer)
    139     else:
    140         # Composite any adjacent images together

/anaconda2/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args, **kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_base.pyc in draw(self, renderer, inframe)
   2397             renderer.stop_rasterizing()
   2398 
-> 2399         mimage._draw_list_compositing_images(renderer, self, artists)
   2400 
   2401         renderer.close_group('axes')

/anaconda2/lib/python2.7/site-packages/matplotlib/image.pyc in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    136     if not_composite or not has_images:
    137         for a in artists:
--> 138             a.draw(renderer)
    139     else:
    140         # Composite any adjacent images together

/anaconda2/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53                 renderer.start_filter()
     54 
---> 55             return draw(artist, renderer, *args, **kwargs)
     56         finally:
     57             if artist.get_agg_filter() is not None:

/anaconda2/lib/python2.7/site-packages/matplotlib/axis.pyc in draw(self, renderer, *args, **kwargs)
   1133         ticks_to_draw = self._update_ticks(renderer)
   1134         ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
-> 1135                                                                 renderer)
   1136 
   1137         for tick in ticks_to_draw:

/anaconda2/lib/python2.7/site-packages/matplotlib/axis.pyc in _get_tick_bboxes(self, ticks, renderer)
   1073         for tick in ticks:
   1074             if tick.label1On and tick.label1.get_visible():
-> 1075                 extent = tick.label1.get_window_extent(renderer)
   1076                 ticklabelBoxes.append(extent)
   1077             if tick.label2On and tick.label2.get_visible():

/anaconda2/lib/python2.7/site-packages/matplotlib/text.pyc in get_window_extent(self, renderer, dpi)
    968             raise RuntimeError('Cannot get window extent w/o renderer')
    969 
--> 970         bbox, info, descent = self._get_layout(self._renderer)
    971         x, y = self.get_unitless_position()
    972         x, y = self.get_transform().transform_point((x, y))

/anaconda2/lib/python2.7/site-packages/matplotlib/text.pyc in _get_layout(self, renderer)
    352         tmp, lp_h, lp_bl = renderer.get_text_width_height_descent('lp',
    353                                                          self._fontproperties,
--> 354                                                          ismath=False)
    355         offsety = (lp_h - lp_bl) * self._linespacing
    356 

/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in get_text_width_height_descent(self, s, prop, ismath)
    233 
    234         flags = get_hinting_flag()
--> 235         font = self._get_agg_font(prop)
    236         font.set_text(s, 0.0, flags=flags)  # the width and height of unrotated string
    237         w, h = font.get_width_height()

/anaconda2/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in _get_agg_font(self, prop)
    270         font = get_font(
    271             fname,
--> 272             hinting_factor=rcParams['text.hinting_factor'])
    273 
    274         font.clear()

/anaconda2/lib/python2.7/site-packages/backports/functools_lru_cache.pyc in wrapper(*args, **kwds)
    135                         stats[HITS] += 1
    136                         return result
--> 137                 result = user_function(*args, **kwds)
    138                 with lock:
    139                     root, = nonlocal_root

RuntimeError: In FT2Font: Can not load face.

<matplotlib.figure.Figure at 0x113232250>

你是否考虑接受被点赞的答案,因为它似乎是有效的? - Dr_Zaszuś
2个回答

10

在使用MacOS上的Anaconda和Spyder与Python 3+时,我遇到了相同的问题。

我不确定这个解决方案是否适用于您,但我通过删除以下所有文件和文件夹来解决它:

/Users/YOUR_USER/.matplotlib

然后在Anaconda Navigator中从我的环境中卸载并重新安装'matplotlib'


我有相同的设置,不过没有删除和重新安装。我只是将字体添加到我的系统中,这导致它被添加到/Users/YOUR_USER/.matplotlib/fontlist-v{version_number}.json文件中。然后在我的python脚本中,我更新了fname以匹配.json文件。 - tubstrr

2
我刚刚遇到了这个错误-查看了matplotlib的调试日志,一切似乎都正常。然而,当我检查已选择的字体路径时,文件大小为0KB。可能在安装matplotlib时发生了损坏。将字体文件从另一台机器复制到字体目录中可以解决问题。顺便说一下,路径是%CONDA_ENV%\Lib\site-packages\matplotlib\mpl-data\fonts\ttf(我在Windows上使用conda,但相对于本地安装的site-packages目录,该路径应该是相同的)。重新安装matplotlib将解决此问题,但最好检查此文件夹,以防问题只是一个坏的字体文件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接