在 Mac OSX 10.6 上的 python 2.7 中导入 matplotlib.mlab 和 .pyplot 时出现问题

发布于 2024-12-01 01:53:01 字数 1186 浏览 1 评论 0原文

我正在尝试在 OSX 10.6 上的 Python 2.7 中使用 matplotlib 绘制直方图

我已经验证可以将 numpy、scipy 和 matplotlib 导入到 python 中。 matplotlib 网站上的示例脚本可以执行此操作

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

但是,我在执行此操作时收到错误。这是我尝试导入 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
>>> 

我做错了什么,无法像脚本那样导入这些内容?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日浅笑〃 2024-12-08 01:53:01

对于ImportError:似乎存在架构不匹配。也许您已经安装了 32 位版本的 matplotlib,但使用的是 64 位 Python?以下 shell 命令打印什么?

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

对于AttributeError:您必须显式导入matplotlib.pyplot,仅导入matplotlib时不会自动导入。最常见的别名方案是:

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

然后您可以使用 plt 名称绘制直方图:

plt.hist(...)

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(...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文