0

I am using the anaconda install of python 2.7 in combination with cygwin in a 64 bit windows interface. As far as I can tell, everything is properly installed, yet when I try to run some example code, I am met with an error.

ImportError: No module named matplotlib.pyplot

Example code:

#!/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()

The above is code to make a contour plot lifted straight from the matplotlib website.

PhysicistAbroad
  • 185
  • 1
  • 8

1 Answers1

1

Chances are, you have multiple Python interpreters installed in different environments on your machine. Did you install the native anaconda for windows, or anaconda under cygwin? If it's the latter and you are running this from cygwin it's probably using the python interpreter at CYGWIN_ROOT/usr/bin/python2.7 (which doesn't have matplotlib) instead of your anaconda install (which does).

I don't use windows, so I'm not positive about the paths, but this post was helpful. From cygwin type:

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

and also change the first part of that script to

#!/usr/bin/env python

so it uses the python set by the export command

Community
  • 1
  • 1
22degrees
  • 596
  • 4
  • 11
  • Yep, looks like it's using the usr/bin/python one instead of my anaconda install. On the other hand, when I typed your code in, it seemed to have difficulty. Heres what I did line by line: which python (enter) export PATH=/cygdrive/c/anaconda:$PATH (enter) and then the which python still reverts to usr/bin/python – PhysicistAbroad Feb 05 '16 at 04:08
  • Wait, got it - my anaconda is at a different directory, but now that I switched over, it worked! – PhysicistAbroad Feb 05 '16 at 04:16
  • /cygdrive/c/anaconda might not exist. I don't know where the default anaconda install goes. It might need /cygdrive/c/anaconda/bin or something. You'll maybe have to dig around in there. You can try 'find /cygdrive/c/anaconda | grep python2.7' and back out the directory that your Python binary lives in from there. – 22degrees Feb 05 '16 at 04:18
  • Right on, glad to help. Add that export command to your .bashrc to set anaconda as your default interpreter from now on (so you don't have to do it every time). – 22degrees Feb 05 '16 at 04:24
  • Oh cool! I found the .bashrc file, but how would I edit this to avoid having to set it every time? Sorry, clearly very new to this game – PhysicistAbroad Feb 05 '16 at 06:27
  • Every line in the .bashrc gets interpreted when you start bash. PATH is a colon separated list of places bash will look for executables. In order. That export command will set your PATH variable to your anaconda install, plus whatever was already there: $PATH (this part is important). So, using your favorite text editor (vim, emacs, nano), add the export command that worked for you to your .bashrc – 22degrees Feb 05 '16 at 06:33