Mehwish
Mehwish

Reputation: 51

Pycharm - Importing Matplotlib

I've installed Pycharm and using Anaconda 3 as my interpreter. I cannot import Matplotlib (or Seaborn). When I run 'import matplotlib.pyplot as plt' I get the following:--

import matplotlib.pyplot as plt

Backend Qt5Agg is interactive backend. Turning interactive mode on.

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'

During handling of the above exception, another exception occurred:

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'])

During handling of the above exception, another exception occurred:

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'])

Upvotes: 4

Views: 31193

Answers (3)

Pratik Dash
Pratik Dash

Reputation: 175

After having faced this issue many times and never having obtained a working solution from all the available answers, I suggest performing a new conda installation from scratch followed by creating a new environment as well. Install matplotlib from there and other packages from there as well. I know it's a bit crude but for people (especially non-experts and newcomers) in a hurry this may be the best solution.

Upvotes: 0

Eskapp
Eskapp

Reputation: 3635

I have just stumbled across the same issue and found a way to solve it easily. I could not make it work with Qt5, so I switched the backend to TkAgg.

First of all, and this is very optional, I updated matplotlib via conda update matplotlib. I did this in order to make sure that I have the backend TkAgg included (however, it has been added in 2014 in conda so...).

Then I changed the backend instruction in the matplotlibrc file that is located in C:\Anaconda3\pkgs\matplotlib-2.0.2-np113py35_0\Lib\site-packages\matplotlib\mpl-data\ if you are using Anaconda3.

Check your version of matplotlib and numpy in Pycharm in Settings -> Project interpreter to make sure that you are modifying the proper file. In my case matplotlib 2.0.2 and numpy 1.13.

Open this matplotlibrc file with an editor and change around line 30 the backend instruction (that is originally Qt5Agg) to 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!!!

Then save, restart PyCharm and your import matplotlib.pyplot as plt should work. You should see that the backend has changed when it starts as it displays: Backend TkAgg is interactive backend. Turning interactive mode on.

(Works with Python 3.5.2, Anaconda 4.2.0 (64-bit))

(Also works with Python 3.6.2, Anaconda 5.5.0 (64-bits))

Upvotes: 4

VanDarkholme
VanDarkholme

Reputation: 75

Try running pip install PyQt5 in the command line (not Python, windows cmd or unix terminal). If this doesn't work - try conda install PyQt5. Though this doesn't make sense as matplotlib uses tkinter.

Upvotes: 4

Related Questions