gpt4 book ai didi

python - 使用 MacOS 的 matplotlib 的哪个后端?

转载 作者:太空宇宙 更新时间:2023-11-03 11:38:05 24 4
gpt4 key购买 nike

在 MacOS 上使用 matplotlib 的问题是一个棘手的问题,已经通过许多讨论(见下文)进行了彻底的审查。问题如下:

  • 使用 MacOS Mojave 10.14.3
  • 在 conda 环境中使用 python 3.7.2
  • 使用 matplotlib 3.0.3

这是我想出的最简单的代码片段,可以重现问题:

from matplotlib import pyplot as plt

x = [1, 2, 3]
y = [1, 2, 3]

plt.plot(x, y)
plt.show()

这会引发以下错误:

2019-03-22 12:25:43.429 python3.7[22209:554135] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7f85866b9de0  
2019-03-22 12:25:43.431 python3.7[22209:554135] \*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7f85866b9de0'
*** First throw call stack:([...])
libc++abi.dylib: terminating with uncaught exception of type NSException

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

问题已记录 here .一种解决方案是将 PyQt5 包安装到您的 Python 安装中,并在脚本的开头添加以下行:

import matplotlib
matplotlib.use("Qt5Agg")

虽然这非常有效,但我想知道为什么其他后端无法提供类似的行为。


我确实尝试过使用 MacOSX 后端:

import matplotlib
matplotlib.use('MACOSX')

产生的错误:

from matplotlib.backends import _macosx  
ImportError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.

问题已记录 here , thereplenty of other threads .

两个解决方案出来了:

  • 安装 python.app(conda install python.app)并使用 pythonw 而不是 python 启动脚本>
  • 使用TKAgg后端

使用第一个效果很好,但我想知道:

  • 为什么我们需要调用 pythonw 而不是 python
  • python.app 包到底是什么?
  • 我们如何使用 IDE(例如 PyCharm)使这个解决方案工作?

至于第二个,它在一定程度上“起作用”:当使用 TkAgg 运行 matplotlib 时,绘图窗口确实有问题。事实上,它通常需要多次点击“缩放”、“平移”或“主页”按钮才能使它们真正起作用。使用它真的是一个巨大的痛苦。我问了几位使用 matplotlibTkAgg 的同事或 friend ,他们都有同样的问题。

有人知道这种情况的原因吗?或者是否有任何解决方法可以避免此问题(除了安装 pyqt5)?

最佳答案

使用第一个选项是最好的选择,因为您已经在使用虚拟环境。根据 matplotlib,python 有两种变体:

  • 框架构建 - 对于 MacOX 中的 GUI 内容非常重要
  • 常规构建。

在这种情况下,Matplotlib 希望与 OSX 本地交互,为此,它需要构建框架,这就是为什么安装 python.app 类型的 python 很重要的原因。更多信息可以从Matplotlib FAQ获得.

检查这个link有关构建 python 框架的需求的更多信息。

关于python - 使用 MacOS 的 matplotlib 的哪个后端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55300038/

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