gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 02:49:39 25 4
gpt4 key购买 nike

我正在尝试在 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
>>>

我做错了什么,我不能像脚本那样导入这些?

最佳答案

对于 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(...)

关于python - 在 Mac OSX 10.6 上的 python 2.7 中导入 matplotlib.mlab 和 .pyplot 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7157330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com