Matplotlib(Jupyter ipython):绘图在MacOS后端不起作用

时间:2017-12-18 13:30:47

标签: python macos matplotlib

问题的背景:我基本上从Macbook pro 2015切换到Macbook pro 2017(从macOS Sierra 10.12.6High Sierra 10.13.2),并且我使用时间机器复制旧macbook中的所有内容。

我最近改变了我的电脑,并且matplotlib -.-
有很多错误 第一个问题是:如果我打开一个互动人物,然后关闭它(按十字架),那么Jupyter会挂起。我查了解解决方案,基本上建议是更新所有内容,所以我做了。

我正在使用

Jupyter 4.4.0
conda 4.3.30
Python 2.7.14 |Anaconda custom (64-bit)| (default, Dec  7 2017, 11:07:58) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin

my previous computer,

conda 4.3.30
Jupyter 4.1.0
Python 2.7.11 | Annaconda custom(x86_64) [GCC 4.2.1 (Apple Inc. build 5577) on darwin

我之前有过matplotlib 1.5.1,现在我升级到2.1.1希望解决问题。

在我之前的计算机中(使用matplotlib仍能正常工作),我使用的是MacOSX后端(默认),它非常适合绘图。但是,我对这个有很多问题。首先,绘图将刚刚崩溃jupyter,现在所有更新至少不会崩溃但是现在, MacOSX后端甚至没有显示情节 <登记/> 细胞刚刚运行,没有任何结果。

示例:

import matplotlib
%matplotlib
import matplotlib.pyplot as plt
print "backend : ", matplotlib.get_backend()
plt.plot(range(10))
plt.show()

输出:

Using matplotlib backend: MacOSX
backend :  MacOSX

根本没有情节!

如果我使用tkAgg后端:

import matplotlib
matplotlib.use('tkAgg')
import matplotlib.pyplot as plt
print "backend : ", matplotlib.get_backend()
plt.plot(range(10))
plt.show()

backend :  TkAgg

它运行正常并在新窗口上绘制绘图,但现在关闭绘图并不会阻止单元格运行。我必须通过键盘停止它或按下jupyter上的停止。

我在这里遗漏了什么吗?无论如何要在matplotlib中解决这个恼人的事情?

此外:

import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt

给出,

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-8c09ccc60eaa> in <module>()
      1 import matplotlib
      2 matplotlib.use('Qt4Agg')
----> 3 import matplotlib.pyplot as plt
      4 print "backend : ", matplotlib.get_backend()
      5 plt.plot(range(10))

//anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py in <module>()
    114 ## Global ##
    115 
--> 116 _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
    117 
    118 _IP_REGISTERED = None

//anaconda/lib/python2.7/site-packages/matplotlib/backends/__init__.pyc in pylab_setup(name)
     58     # imports. 0 means only perform absolute imports.
     59     backend_mod = __import__(backend_name, globals(), locals(),
---> 60                              [backend_name], 0)
     61 
     62     # Things we pull in from all backends

//anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_qt4agg.py in <module>()
      8 
      9 from .backend_agg import FigureCanvasAgg
---> 10 from .backend_qt4 import (
     11     QtCore, _BackendQT4, FigureCanvasQT, FigureManagerQT, NavigationToolbar2QT)
     12 from .backend_qt5agg import FigureCanvasQTAggBase

//anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_qt4.py in <module>()
     16 from matplotlib.widgets import SubplotTool
     17 
---> 18 from .qt_compat import QtCore, QtWidgets, _getSaveFileName, __version__
     19 
     20 from .backend_qt5 import (

//anaconda/lib/python2.7/site-packages/matplotlib/backends/qt_compat.py in <module>()
    148     # have been changed in the above if block
    149     if QT_API in [QT_API_PYQT, QT_API_PYQTv2]:  # PyQt4 API
--> 150         from PyQt4 import QtCore, QtGui
    151 
    152         try:

ImportError: dlopen(/usr/local/lib/python2.7/site-packages/PyQt4/QtCore.so, 2): Library not loaded: /usr/local/lib/QtCore.framework/Versions/4/QtCore
  Referenced from: /usr/local/lib/python2.7/site-packages/PyQt4/QtCore.so
  Reason: image not found

1 个答案:

答案 0 :(得分:0)

我的解决方法是将图像保存到文件,然后通过PIL

打开它
plt.plot(x,y)
plt.save('fig.png')

from PIL import Image
img = Image.open('fig.png')
img.show()