我在64位Windows界面中使用anaconda安装的python 2.7与cygwin结合使用。据我所知,一切都已正确安装,但当我尝试运行一些示例代码时,我遇到了错误。
ImportError:没有名为matplotlib.pyplot的模块
示例代码:
#!/usr/bin/python2.7
'''
Demonstrate use of a log color scale in contourf
'''
import matplotlib.pyplot as plt
import numpy as np
from numpy import ma
from matplotlib import colors, ticker, cm
from matplotlib.mlab import bivariate_normal
N = 100
x = np.linspace(-3.0, 3.0, N)
y = np.linspace(-2.0, 2.0, N)
X, Y = np.meshgrid(x, y)
# A low hump with a spike coming out of the top right.
# Needs to have z/colour axis on a log scale so we see both hump and spike.
# linear scale only shows the spike.
z = (bivariate_normal(X, Y, 0.1, 0.2, 1.0, 1.0)
+ 0.1 * bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0))
# Put in some negative values (lower left corner) to cause trouble with logs:
z[:5, :5] = -1
# The following is not strictly essential, but it will eliminate
# a warning. Comment it out to see the warning.
z = ma.masked_where(z <= 0, z)
# Automatic selection of levels works; setting the
# log locator tells contourf to use a log scale:
cs = plt.contourf(X, Y, z, locator=ticker.LogLocator(), cmap=cm.PuBu_r)
# Alternatively, you can manually set the levels
# and the norm:
#lev_exp = np.arange(np.floor(np.log10(z.min())-1),
# np.ceil(np.log10(z.max())+1))
#levs = np.power(10, lev_exp)
#cs = P.contourf(X, Y, z, levs, norm=colors.LogNorm())
# The 'extend' kwarg does not work yet with a log scale.
cbar = plt.colorbar()
plt.show()
以上是使用matplotlib网站直接提升轮廓图的代码。
答案 0 :(得分:1)
很可能,您的计算机上的不同环境中安装了多个Python解释器。您是否在cygwin下安装了本机anaconda for windows或anaconda?如果它是后者并且你是从cygwin运行的,它可能使用CYGWIN_ROOT / usr / bin / python2.7(它没有matplotlib)的python解释器而不是你的anaconda安装(确实如此)。
我没有使用Windows,所以我对这些路径并不乐观,但this post很有帮助。来自cygwin类型:
$ which python
$ export PATH=/cygdrive/c/anaconda:$PATH
$ which python
并将该脚本的第一部分更改为
#!/usr/bin/env python
所以它使用了导出命令
设置的python