Pycharm -导入Matplotlib

nxagd54h  于 2023-06-06  发布在  PyCharm
关注(0)|答案(3)|浏览(160)

我已经安装了Pycharm并使用Anaconda 3作为我的解释器。我无法导入Matplotlib(或Seaborn)。当我运行'import matplotlib.pyplot as plt'时,我得到以下结果:

import matplotlib.pyplot as plt

后端Qt5Agg是交互式后端。打开交互模式。

Traceback (most recent call last):
 
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition
2016.2.3\helpers\pydev\pydev_ipython\inputhook.py", line 502, in enable_gui
    gui_hook = guis[gui] KeyError: 'qt5'

在处理上述异常的过程中,又出现了一个异常:

Traceback (most recent call last):
      File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2.3\helpers\pydev\_pydev_bundle\pydev_ipython_console_011.py", line 123, in enable_gui
        return real_enable_gui(gui, app)
      File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2.3\helpers\pydev\pydev_ipython\inputhook.py", line 508, in enable_gui
        raise ValueError(e)
    ValueError: Invalid GUI request 'qt5', valid ones are:dict_keys(['osx', 'gtk', 'qt4', 'qt', 'gtk3', 'glut', 'pyglet',
'wx', 'none', 'tk'])

在处理上述异常的过程中,又出现了一个异常:

Traceback (most recent call last):
  File "C:\Program Files\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-eff513f636fd>", line 1, in <module>
    import matplotlib.pyplot as plt
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2.3\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Program Files\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 2512, in <module>
    install_repl_displayhook()
  File "C:\Program Files\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 165, in install_repl_displayhook
    ip.enable_gui(ipython_gui_name)
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2.3\helpers\pydev\_pydev_bundle\pydev_ipython_console_011.py", line 125, in enable_gui
    raise UsageError("%s" % e)
IPython.core.error.UsageError: Invalid GUI request 'qt5', valid ones are:dict_keys(['osx', 'gtk', 'qt4', 'qt', 'gtk3', 'glut', 'pyglet', 'wx', 'none', 'tk'])
bz4sfanl

bz4sfanl1#

尝试在命令行中运行pip install PyQt5(而不是Python、windows cmd或unix终端)。如果这不起作用-尝试conda install PyQt5。虽然这没有意义,因为matplotlib使用tkinter。

knsnq2tg

knsnq2tg2#

我刚刚偶然发现了同样的问题,并找到了一种轻松解决的方法。我无法让它与Qt 5一起工作,所以我将后端切换到TkAgg。
首先,这是非常可选的,我通过conda update matplotlib更新了matplotlib。我这样做是为了确保我有后端TkAgg包括在内(然而,它已经在2014年添加到conda中,所以...)。
然后,我更改了C:\Anaconda3\pkgs\matplotlib-2.0.2-np113py35_0\Lib\site-packages\matplotlib\mpl-data\中的matplotlibrc文件中的后端指令(如果您使用的是Anaconda 3)。
在设置->项目解释器中检查Pycharm中matplotlibnumpy的版本,以确保您正在修改正确的文件。在我的例子中是matplotlib 2.0.2numpy 1.13
用编辑器打开这个matplotlibrc文件,并在第30行附近将后端指令(最初是Qt5Agg)更改为TkAgg

#### CONFIGURATION BEGINS HERE

# The default backend; one of GTK GTKAgg GTKCairo GTK3Agg GTK3Cairo
# MacOSX Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG
# Template.
# You can also deploy your own backend outside of matplotlib by
# referring to the module name (which must be in the PYTHONPATH) as
# 'module://my_backend'.
backend      : TkAgg  # HERE!!!

然后保存,重新启动PyCharm,import matplotlib.pyplot as plt应该可以工作。您应该看到后端在启动时发生了更改,如下所示:Backend TkAgg is interactive backend. Turning interactive mode on.
(适用于Python 3.5.2、Anaconda 4.2.0(64位))
(Also适用于Python 3.6.2、Anaconda 5.5.0(64位))

yptwkmov

yptwkmov3#

在多次面对这个问题并且从未从所有可用的答案中获得工作解决方案之后,我建议从头开始执行新的conda安装,然后创建新的环境。从那里安装matplotlib和其他软件包。我知道这有点粗糙,但对于匆忙的人(特别是非Maven和新手)来说,这可能是最好的解决方案。

相关问题