首先需要安装pip,sudo easy_install pip
numpy安装
pip install -U numpy
有没有-U视情况而定
matplotlib安装
pip install matplotlib
,如果没有权限,需要在前面加上sudo
。有时候会提示一个-H
有关的东西,这时候可以尝试sudo -H pip install matplotlib
安装。安装完成后进入python,键入import matplotlib
即可开始matplotlib的使用。我在键入后出现了如下问题:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/matplotlib/__init__.py", line 1131, in <module>
rcParams = rc_params()
File "/Library/Python/2.7/site-packages/matplotlib/__init__.py", line 975, in rc_params
return rc_params_from_file(fname, fail_on_error)
File "/Library/Python/2.7/site-packages/matplotlib/__init__.py", line 1100, in rc_params_from_file
config_from_file = _rc_params_in_file(fname, fail_on_error)
File "/Library/Python/2.7/site-packages/matplotlib/__init__.py", line 1018, in _rc_params_in_file
with _open_file_or_url(fname) as fd:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/Library/Python/2.7/site-packages/matplotlib/__init__.py", line 1000, in _open_file_or_url
encoding = locale.getdefaultlocale()[1]
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/locale.py", line 543, in getdefaultlocale
return _parse_localename(localename)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/locale.py", line 475, in _parse_localename
raise ValueError, 'unknown locale: %s' % localename
ValueError: unknown locale: UTF-8
具体解决办法参见stackoverflow
先Ctrl+D退出python,然后键入locale
,此时我的电脑显示:
LANG=
LC_COLLATE="C"
LC_CTYPE="UTF-8"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL=
然后键入
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
此时键入locale
,可以查看一下显示均变为en_US.UTF-8
。这时键入import matplotlib
错误消失。但是下次运行terminal还是需要重新设置。
终极办法,进入/etc/profile
文件,在文件最末端加入
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
目前不知道是否会导致别的错误。