2016-11-22 22 views
1

我使用的是Mac,AttributeError的:「模塊」對象有沒有屬性「風格」

我嘗試使用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 所以我想這個

​​

,這裏是錯誤

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

哪個版本的matplotlib是它? – Chr

+0

@ C.Dlp說實話,我不知道,我不知道如何檢查它,我也沒有看過這個,但沒有幫助我知道它是哪個版本http://stackoverflow.com/questions/ 21473600/matplotlib-version –

+0

試試這個:'from matplotlib.style import use''use('ggplot')' – Chr

回答

2

一種方式是做

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

您必須使用--ignore-installed六因爲操作系統附帶六個1.4.1安裝,如果試圖卸載它(因爲awscli取決於botocore,botocore取決於python-dateutil和python-dateutil取決於六> = 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.') 

提到的錯誤,那麼很容易解決它

open terminal

然後寫python

然後

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

一旦你知道路徑,進入finder ----->Go 還有搜索文件夾,刪除fontList

然後再次運行您的程序,您將再次收到警告並解決問題。 更多信息,可以在這裏找到第二部分 matplotlib taking time when being imported

相關問題