AttributeError:'模块'对象没有属性' style'

时间:2016-11-22 14:34:31

标签: python matplotlib module

我正在使用Mac,

我尝试使用ggplot如下

import matplotlib.pyplot as plt
plt.style.use('ggplot')

但我总是收到像

这样的错误
File "/Users/Desktop/code.py", line 4, in <module>
    plt.style.use('ggplot')
AttributeError: 'module' object has no attribute 'style'

我一直在检查找到解决方案,但我不知道我在哪里弄错了。有评论吗?

如上所述,我必须升级matplotlib但是,我收到了错误 我的python版本是2.7 所以我试过这个

pip2 install matplotlib

这是错误

Requirement already satisfied: matplotlib in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
Requirement already satisfied: numpy>=1.5 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from matplotlib)
Requirement already satisfied: python-dateutil in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from matplotlib)
Requirement already satisfied: tornado in /Library/Python/2.7/site-packages (from matplotlib)
Requirement already satisfied: pyparsing>=1.5.6 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from matplotlib)
Requirement already satisfied: nose in /Library/Python/2.7/site-packages (from matplotlib)
Requirement already satisfied: singledispatch in /Library/Python/2.7/site-packages/singledispatch-3.4.0.3-py2.7.egg (from tornado->matplotlib)
Requirement already satisfied: certifi in /Library/Python/2.7/site-packages (from tornado->matplotlib)
Requirement already satisfied: backports_abc>=0.4 in /Library/Python/2.7/site-packages (from tornado->matplotlib)
Requirement already satisfied: six in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from singledispatch->tornado->matplotlib)

或者我试过

sudo port install py27-pip

这是错误

Password:
Error: Current platform "darwin 16" does not match expected platform "darwin 15"
Error: If you upgraded your OS, please follow the migration instructions: https://trac.macports.org/wiki/Migration
OS platform mismatch
    while executing
"mportinit ui_options global_options global_variations"
Error: /opt/local/bin/port: Failed to initialize MacPorts, OS platform mismatch

或者我试过这个

python get-pip.py
#Requirement already up-to-date: pip in /Library/Python/2.7/site-packages

然后

pip install matplotlib

错误如下

Requirement already satisfied: matplotlib in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
Requirement already satisfied: numpy>=1.5 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from matplotlib)
Requirement already satisfied: python-dateutil in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from matplotlib)
Requirement already satisfied: tornado in /Library/Python/2.7/site-packages (from matplotlib)
Requirement already satisfied: pyparsing>=1.5.6 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from matplotlib)
Requirement already satisfied: nose in /Library/Python/2.7/site-packages (from matplotlib)
Requirement already satisfied: singledispatch in /Library/Python/2.7/site-packages/singledispatch-3.4.0.3-py2.7.egg (from tornado->matplotlib)
Requirement already satisfied: certifi in /Library/Python/2.7/site-packages (from tornado->matplotlib)
Requirement already satisfied: backports_abc>=0.4 in /Library/Python/2.7/site-packages (from tornado->matplotlib)
Requirement already satisfied: six in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from singledispatch->tornado->matplotlib)


pip2 install -U matplotlib

sudo pip2 install -U matplotlib

它给我带来了很多错误,只是在底部吹了几行

('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pytz/zoneinfo', '/var/folders/0y/736q6sd901z27bmpx9nwn_p40000gn/T/pip-r5YiwW-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pytz/zoneinfo', "[Errno 1] Operation not permitted: '/var/folders/0y/736q6sd901z27bmpx9nwn_p40000gn/T/pip-r5YiwW-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pytz/zoneinfo'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pytz', '/var/folders/0y/736q6sd901z27bmpx9nwn_p40000gn/T/pip-r5YiwW-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pytz', "[Errno 1] Operation not permitted: '/var/folders/0y/736q6sd901z27bmpx9nwn_p40000gn/T/pip-r5YiwW-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pytz'")]

如果我使用sudo

,则相同
"[Errno 1] Operation not permitted: '/tmp/pip-5PMqIK-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pytz'")]

1 个答案:

答案 0 :(得分:1)

一种方法是做

sudo pip2 install -U matplotlib --ignore-installed six

你必须使用 - ignore-installed six ,因为OS安装了六个1.4.1并且如果试图卸载它(因为awscli依赖于botocore,botocore依赖于python -dateutil和python-dateutil依赖于六个&gt; = 1.5)它没有权限这样做,因为系统完整性保护甚至不允许root修改这些目录。

此处突出显示https://github.com/pypa/pip/issues/3165

执行此操作后,您可以将matplotlib的版本检查为

python -c 'import matplotlib; print matplotlib.__version__'

如果您收到错误提及

/usr/local/lib/python2.7/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')

,然后很容易解决它

  

打开终端

然后写python

然后

>>> import matplotlib as mpl
>>> mpl.get_configdir()

了解完路径后,请转到finder -----&gt; Go 搜索文件夹并删除fontList

然后再次运行程序,您将再次收到警告并解决。 更多信息,也可以在这里找到第二部分 matplotlib taking time when being imported