【问题标题】:Continuum Anaconda and matplotlibContinuum Anaconda 和 matplotlib
【发布时间】:2016-02-05 03:43:57
【问题描述】:

我在 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,然后import sysprint(sys.path)。会打印什么?

标签: python matplotlib anaconda


【解决方案1】:

很可能,您的机器上的不同环境中安装了多个 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

【讨论】:

  • 是的,看起来它使用的是 usr/bin/python 而不是我的 anaconda 安装。另一方面,当我输入你的代码时,它似乎有困难。这是我逐行所做的: which python (enter) export PATH=/cygdrive/c/anaconda:$PATH (enter) 然后 which python 仍然恢复为 usr/bin/python
  • 等等,明白了——我的 anaconda 位于不同的目录,但现在我切换了,它工作了!
  • /cygdrive/c/anaconda 可能不存在。我不知道默认的 anaconda 安装在哪里。它可能需要 /cygdrive/c/anaconda/bin 什么的。你可能不得不在那里挖掘。你可以试试 'find /cygdrive/c/anaconda | grep python2.7' 并从那里退出 Python 二进制文件所在的目录。
  • 好的,很高兴为您提供帮助。从现在开始,将该导出命令添加到您的 .bashrc 以将 anaconda 设置为您的默认解释器(因此您不必每次都这样做)。
  • 太酷了!我找到了 .bashrc 文件,但我将如何编辑它以避免每次都设置它?抱歉,对这个游戏很陌生
猜你喜欢
  • 2019-05-09
  • 2023-03-28
  • 2015-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-11
  • 1970-01-01
相关资源
最近更新 更多