我目前正在练习matplotlib。这是我练习的第一个例子。

#!/usr/bin/python

import matplotlib.pyplot as plt

radius = [1.0, 2.0, 3.0, 4.0]
area = [3.14159, 12.56636, 28.27431, 50.26544]

plt.plot(radius, area)
plt.show()

当我用python ./plot_test.py运行这个脚本时,它正确地显示了plot。然而,我自己运行它,./plot_test.py,它抛出以下内容:

Traceback (most recent call last):
  File "./plot_test.py", line 3, in <module>
    import matplotlib.pyplot as plt
ImportError: No module named matplotlib.pyplot

python是否在不同的位置查找matplotlib ?

环境是:

Mac OS X 10.8.4 64bit 内置python 2.7

Numpy, scipy, matplotlib安装:

sudo port install py27-numpy py27-scipy py27-matplotlib \
py27-ipython +notebook py27-pandas py27-sympy py27-nose

当前回答

我把我的头砸了几个小时,直到我想到检查我的.bash_profile。我没有为python3列出路径,所以我添加了以下代码:

# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH

然后重新安装matplotlib用sudo pip3安装matplotlib。现在一切都运转得很好。

其他回答

我把我的头砸了几个小时,直到我想到检查我的.bash_profile。我没有为python3列出路径,所以我添加了以下代码:

# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH

然后重新安装matplotlib用sudo pip3安装matplotlib。现在一切都运转得很好。

我有一个类似的问题,使用pip3和所有这些东西都可以安装matplotlib,但不能安装pyplot。这为我解决了问题:

import matplotlib as plt
from matplotlib import pyplot as pllt

受到Sheetal Kaul的启发,这个方法对我很有效

pip uninstall matplotlib
python3 -m pip install matplotlib

当它工作时,我知道它安装在错误的地方:

python2.7
import matplotlib

首先检查Python的版本

python2:

sudo apt-get install python-matplotlib

python3:

sudo apt-get install python3-matplotlib

如果您不匹配Matplotlib安装和Python版本,您将得到no-module错误,因为该版本没有模块退出。

通过这样做,可以在python 3中安装matplotlib包

python3 -m pip install matplotlib --user

这对我很有用。