解決Pycharm無法顯示matplotlib繪圖問題,pycharmmatplotlib
最近開始接觸matplotlib,
1.首先安裝matplotlib庫和其依賴的一些其他庫,例如:numpy,scipy和pandas等
2.開始進行簡單的編碼工作,並在PyCharm中運行,出現如下錯誤:
Traceback (most recent call last): File "J:/MyGit/pythonTest/GetTegeather/test06.py", line 4, in <module> import matplotlib.pyplot as plt File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\pyplot.py", line 115, in <module> _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\backends\__init__.py", line 62, in pylab_setup [backend_name], 0) File "J:\software\PyCharm 2017.3.1\helpers\pycharm_matplotlib_backend\backend_interagg.py", line 17, in <module> verbose = matplotlib.verboseAttributeError: module 'matplotlib' has no attribute 'verbose'
解決步驟如下:
前提:
1.匯入正確版本的matplotlib庫
2.代碼最後調用matplotlibku中pyplot.show()方法
3,無其他低級錯誤
解決步驟:
1.在匯入pyplot之前.需要輸入如下步驟:
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
....
2.此時運行python程式,雖然不報錯,但是並沒有繪圖介面彈出
在命令號幾面輸入如下命令:
import matplotlib
print(matplotlib.get_backend())
將得到的字串結果替換mlp.use('Agg')中Agg'即可
例如:我的print(matplotlib.get_backend())結果為TkAgg,替換後為:
mpl.use('TkAgg')
3.顯示正常
4.至於原因,個人能力有限,無法解釋透徹,
有大神直接更改matplotlibrc檔案中backend的值(backend選項改成你系統的圖形環境),有興趣的自行摸索