Mac環境でPython3.4にmatplotlibをインストールする方法

Pythonのライブラリ、matplotlibをMac環境にインストールする手順です。

環境

手順

freetypeとlibpngをインストールする
% brew install freetype
 
==> Installing freetype dependency: libpng
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/libpng-1.6.14.mountain_lion.bottle.tar.gz
######################################################################## 100.0%
==> Pouring libpng-1.6.14.mountain_lion.bottle.tar.gz
🍺  /usr/local/Cellar/libpng/1.6.14: 17 files, 1.3M
==> Installing freetype
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/freetype-2.5.3_1.mountain_lion.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring freetype-2.5.3_1.mountain_lion.bottle.1.tar.gz
🍺  /usr/local/Cellar/freetype/2.5.3_1: 60 files, 2.6M
matplotlibをインストールする
pip3 install matplotlib
matplotlibをインストールする
sudo ARCHFLAGS=-Wno-unused-result pip3 install matplotlib

動作確認

下記のコマンドを実行し円グラフが表示されれば、正常にインストールが完了しています。

matplotlibの動作確認
python3 -c 'import matplotlib.pyplot as pyplot; pyplot.pie([1,2,3]); pyplot.show()'

インストールエラーが発生するときは

下記のようなエラーが発生するときは、freetypeやlibpngのインストールの有無、コンパイルオプションの有無などを確認しましょう。

% pip3 install matplotlib
 
Downloading/unpacking matplotlib
  Downloading matplotlib-1.4.2.tar.gz (50.1MB): 50.1MB downloaded
  Running setup.py (path:/private/var/folders/dy/ffm11v7n3_16xbw15wlbxwbm0000gn/T/pip_build_zero/matplotlib/setup.py) egg_info for package matplotlib
    Traceback (most recent call last):
      File "<string>", line 17, in <module>
      File "/private/var/folders/dy/ffm11v7n3_16xbw15wlbxwbm0000gn/T/pip_build_zero/matplotlib/setup.py", line 155, in <module>
        result = package.check()
      File "/private/var/folders/dy/ffm11v7n3_16xbw15wlbxwbm0000gn/T/pip_build_zero/matplotlib/setupext.py", line 962, in check
        min_version='2.3', version=version)
      File "/private/var/folders/dy/ffm11v7n3_16xbw15wlbxwbm0000gn/T/pip_build_zero/matplotlib/setupext.py", line 446, in _check_for_pkg_config
        if (not is_min_version(version, min_version)):
      File "/private/var/folders/dy/ffm11v7n3_16xbw15wlbxwbm0000gn/T/pip_build_zero/matplotlib/setupext.py", line 174, in is_min_version
        return found_version >= expected_version
      File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/distutils/version.py", line 76, in __ge__
        c = self._cmp(other)
      File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/distutils/version.py", line 343, in _cmp
        if self.version < other.version:
    TypeError: unorderable types: str() < int()
    ============================================================================
    Edit setup.cfg to change the build options
 
    BUILDING MATPLOTLIB
                matplotlib: yes [1.4.2]
                    python: yes [3.4.2 (default, Oct 19 2014, 17:50:17)  [GCC
                            4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)]]
                  platform: yes [darwin]
 
    REQUIRED DEPENDENCIES AND EXTENSIONS
                     numpy: yes [version 1.9.1]
                       six: yes [six was not found.]
                  dateutil: yes [dateutil was not found. It is required for date
                            axis support. pip/easy_install may attempt to
                            install it after matplotlib.]
                      pytz: yes [pytz was not found. pip will attempt to install
                            it after matplotlib.]
                   tornado: yes [tornado was not found. It is required for the
                            WebAgg backend. pip/easy_install may attempt to
                            install it after matplotlib.]
                 pyparsing: yes [pyparsing was not found. It is required for
                            mathtext support. pip/easy_install may attempt to
                            install it after matplotlib.]
                     pycxx: yes [Official versions of PyCXX are not compatible
                            with matplotlib on Python 3.x, since they lack
                            support for the buffer object.  Using local copy]
                    libagg: yes [pkg-config information for 'libagg' could not
                            be found. Using local copy.]
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
 
  File "<string>", line 17, in <module>
 
  File "/private/var/folders/dy/ffm11v7n3_16xbw15wlbxwbm0000gn/T/pip_build_zero/matplotlib/setup.py", line 155, in <module>
 
    result = package.check()
 
  File "/private/var/folders/dy/ffm11v7n3_16xbw15wlbxwbm0000gn/T/pip_build_zero/matplotlib/setupext.py", line 962, in check
 
    min_version='2.3', version=version)
 
  File "/private/var/folders/dy/ffm11v7n3_16xbw15wlbxwbm0000gn/T/pip_build_zero/matplotlib/setupext.py", line 446, in _check_for_pkg_config
 
    if (not is_min_version(version, min_version)):
 
  File "/private/var/folders/dy/ffm11v7n3_16xbw15wlbxwbm0000gn/T/pip_build_zero/matplotlib/setupext.py", line 174, in is_min_version
 
    return found_version >= expected_version
 
  File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/distutils/version.py", line 76, in __ge__
 
    c = self._cmp(other)
 
  File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/distutils/version.py", line 343, in _cmp
 
    if self.version < other.version:
 
TypeError: unorderable types: str() < int()
 
============================================================================
 
Edit setup.cfg to change the build options
 
 
 
BUILDING MATPLOTLIB
 
            matplotlib: yes [1.4.2]
 
                python: yes [3.4.2 (default, Oct 19 2014, 17:50:17)  [GCC
 
                        4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)]]
 
              platform: yes [darwin]
 
 
 
REQUIRED DEPENDENCIES AND EXTENSIONS
 
                 numpy: yes [version 1.9.1]
 
                   six: yes [six was not found.]
 
              dateutil: yes [dateutil was not found. It is required for date
 
                        axis support. pip/easy_install may attempt to
 
                        install it after matplotlib.]
 
                  pytz: yes [pytz was not found. pip will attempt to install
 
                        it after matplotlib.]
 
               tornado: yes [tornado was not found. It is required for the
 
                        WebAgg backend. pip/easy_install may attempt to
 
                        install it after matplotlib.]
 
             pyparsing: yes [pyparsing was not found. It is required for
 
                        mathtext support. pip/easy_install may attempt to
 
                        install it after matplotlib.]
 
                 pycxx: yes [Official versions of PyCXX are not compatible
 
                        with matplotlib on Python 3.x, since they lack
 
                        support for the buffer object.  Using local copy]
 
                libagg: yes [pkg-config information for 'libagg' could not
 
                        be found. Using local copy.]
 
----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /private/var/folders/dy/ffm11v7n3_16xbw15wlbxwbm0000gn/T/pip_build_zero/matplotlib
Storing debug log for failure in /Users/zero/.pip/pip.log

関連