LoginSignup
7

More than 5 years have passed since last update.

Python で matplotlib を試そうとしたら 'cairo.Context' ってナニソレ

Posted at

1. 突然の 'cairo.Context'

>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax1 = fig.add_subplot(2,2,1)
>>> plt.show()
TypeError: Couldn't find foreign struct converter for 'cairo.Context'

 何さこれ... せっかくグラフ書いたりして遊ぼうと思ってたのに...

2. わたしの環境

ディストリビューション:4.4.5-1-ARCH
Python:Python 3.5.1

3. 解決法は stackoverflow が知っていた

Python matplotlib Cairo errorという記事に同様の例があったのでソレにならってみるとあっさり解決することができた。

  • まず python3-pyqt5 をインストールする。
# arch の公式リポジトリには python3-pyqt5 ではなく python-pyqt5 としてある。
sudo pacman -S python-pyqt5
  • matplotlibrc を書き換えるために,ディレクトリを調べる。
>>>import matplotlib
>>>matplotlib.matplotlib_fname()
'/usr/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc'
  • backend : の項目を書き換える。
matplotlibrc
# The default backend; one of GTK GTKAgg GTKCairo GTK3Agg GTK3Cairo
# CocoaAgg MacOSX Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG
# Template.
# You can also deploy your own backend outside of matplotlib by
# referring to the module name (which must be in the PYTHONPATH) as
# 'module://my_backend'.
backend      : qt5agg # ここを書き換えた。もとは backend : gtk3agg
  • そして...

 できた。やったぜ。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
7