Continuum Anaconda 和 matplotlib

Continuum Anaconda and matplotlib

我在 64 位 windows 界面中将 python 2.7 的 anaconda 安装与 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 网站提取等高线图的代码。

很有可能,您在计算机的不同环境中安装了多个 Python 解释器。你安装的是windows原生的anaconda还是cygwin下的anaconda?如果是后者,而你是来自 cygwin 的 运行,它可能正在使用 CYGWIN_ROOT/usr/bin/python2.7 处的 python 解释器(没有 matplotlib),而不是你的 anaconda 安装(有) .

我不使用 windows,所以我对这些路径不太满意,但 this post 很有帮助。来自 cygwin 类型:

$ which python
$ export PATH=/cygdrive/c/anaconda:$PATH
$ which python

并将该脚本的第一部分更改为

#!/usr/bin/env python

所以它使用导出命令设置的python