4

I am trying to plot a histogram using matplotlib in Python 2.7 on OSX 10.6

I have verified that I can import numpy, scipy, and matplotlib into python. A sample script on the matplotlib website does

#!/usr/bin/env python
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

However, I get an error when doing this. Here is what happens when I try to import mlab.

Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.mlab as mlab
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/mlab.py", line 151, in <module>
    import matplotlib.nxutils as nxutils
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/nxutils.so, 2): no suitable image found.  Did find:
    /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/nxutils.so: no matching architecture in universal wrapper
>>> 

What am I doing wrong that I can't import these as the script does?

1
  • A related issue: If I try to call matplotlib.pyplot.hist() it doesn't work and gives me an AttributeError saying matplotlib has no attribute 'pyplot', but this is listed in the matplotlib documentation. Commented Aug 23, 2011 at 7:14

1 Answer 1

3

For the ImportError: It seems that there is an architecture mismatch. Maybe you have installed a 32-bit version of matplotlib, but are using a 64-bit Python? What does the following shell command print?

file /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/nxutils.so

For the AttributeError: You have to explicitely import matplotlib.pyplot, it won't get imported automatically when just importing matplotlib. The most common aliasing scheme is:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

Then you can draw your histogram using the plt name:

plt.hist(...)
Sign up to request clarification or add additional context in comments.

2 Comments

I think you are correct. I looked into it a little and apparently I am running 64-bit Python. The output from your question is: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/nxutils.so: Mach-O universal binary with 2 architectures /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/nxutils.so (for architecture ppc): Mach-O bundle ppc /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/nxutils.so (for architecture i386): Mach-O bundle i386
Is the best option to run python as 32-bit? How would I do this? I also tried looking for a 64-bit matplotlib, but it seemed that to do this for snow leopard is somewhat involved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.