web-dev-qa-db-ja.com

Python 3 pylabでのMatplotlibによるプロット:TkinterおよびQt FontManagerエラー

Ubuntu 14.04のPython 3でpylab(つまり、$ ipython3 --pylab)を使用しようとしています。 Tkinterバックエンドを使用するかQt4バックエンドを使用するかにかかわらず、同じエラーが発生します。

AttributeError: 'FontManager' object has no attribute 'ttf_lookup_cache'

ただし、これと同じ基本的なプロットルーチン(plt.plot([1,2,3,4]))は、Python 2 pylabで機能します。

私の知る限り、必要な依存関係をインストールしました(Python 2とPython 3の両方に注意してください):

Sudo apt-get install python-numpy python-scipy python-matplotlib
Sudo apt-get install python3-numpy python3-scipy python3-matplotlib
Sudo apt-get install python-tk python3-tk
Sudo apt-get install libqt4gui libqt4core python-qt4 python3-qt4

完全なトレースバックは以下から入手できます。 Tkinterの場合:

$ ipython3 --matplotlib tk
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
Type "copyright", "credits" or "license" for more information.

IPython 1.2.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import matplotlib.pyplot as plt

In [2]: plt.plot([1,2,3,4])
Out[2]: [<matplotlib.lines.Line2D at 0x7f1496dc9c50>]

In [3]: Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1490, in __call__
    return self.func(*args)
  File "/usr/lib/python3.4/tkinter/__init__.py", line 535, in callit
    func(*args)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 363, in idle_draw
    self.draw()
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line 348, in draw
    FigureCanvasAgg.draw(self)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_agg.py", line 451, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 56, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/figure.py", line 1035, in draw
    func(*args)
  File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 56, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/axes.py", line 2088, in draw
    a.draw(renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 56, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/axis.py", line 1094, in draw
    renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/axis.py", line 1043, in _get_tick_bboxes
    extent = tick.label1.get_window_extent(renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/text.py", line 755, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/text.py", line 321, in _get_layout
    ismath=False)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_agg.py", line 214, in get_text_width_height_descent
    font = self._get_agg_font(prop)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_agg.py", line 258, in _get_agg_font
    fname = findfont(prop)
  File "/usr/lib/python3/dist-packages/matplotlib/font_manager.py", line 1362, in findfont
    font = fontManager.findfont(prop, **kw)
  File "/usr/lib/python3/dist-packages/matplotlib/font_manager.py", line 1202, in findfont
    font_cache = self.ttf_lookup_cache
AttributeError: 'FontManager' object has no attribute 'ttf_lookup_cache'

そして、ここにQt/Qt4があります:

$ ipython3 --matplotlib qt
WARNING: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
Type "copyright", "credits" or "license" for more information.

IPython 1.2.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import matplotlib.pyplot as plt

In [2]: plt.plot([1,2,3,4])
Out[2]: [<matplotlib.lines.Line2D at 0x7f0f840947f0>]

In [3]: Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_qt4.py", line 374, in idle_draw
    self.draw()
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_qt4agg.py", line 154, in draw
    FigureCanvasAgg.draw(self)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_agg.py", line 451, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 56, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/figure.py", line 1035, in draw
    func(*args)
  File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 56, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/axes.py", line 2088, in draw
    a.draw(renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 56, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/axis.py", line 1094, in draw
    renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/axis.py", line 1043, in _get_tick_bboxes
    extent = tick.label1.get_window_extent(renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/text.py", line 755, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/text.py", line 321, in _get_layout
    ismath=False)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_agg.py", line 214, in get_text_width_height_descent
    font = self._get_agg_font(prop)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_agg.py", line 258, in _get_agg_font
    fname = findfont(prop)
  File "/usr/lib/python3/dist-packages/matplotlib/font_manager.py", line 1362, in findfont
    font = fontManager.findfont(prop, **kw)
  File "/usr/lib/python3/dist-packages/matplotlib/font_manager.py", line 1202, in findfont
    font_cache = self.ttf_lookup_cache
AttributeError: 'FontManager' object has no attribute 'ttf_lookup_cache'

If you suspect this is an IPython bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at [email protected]

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
    %config Application.verbose_crash=True

どちらの場合も、プロットウィンドウが表示されます。 Tkinterの場合、プロットは空白(空)ですが、Qt4では正しい結果が表示されます。エラーが発生します。繰り返しますが、これはPython 2.7のいずれかのバックエンドで正常に機能します。 私は何が欠けていますか?

4
Arthur

Matplotlibフォントキャッシュ(Python 3およびPython 2の両方のフォントキャッシュ)を削除することで、この問題を解決できました。

rm /home/arthur/.cache/matplotlib/fontList*.cache

エラーなしで以下の環境初期化を使用できるようになりました。

ipython3 --pylab
ipython3 --matplotlib tk
ipython3 --matplotlib qt4
6
Arthur