2

I need to create scientific plots with matplotlib in Jupyter notebook, and create them in separate windows instead of inline. I ran %matplotlib qt and got this error: ImportError: No module named PyQt4

A lot of related answers on the internet involves installing PyQt4. To give some background information, I use Anaconda to manage python modules on a Windows machine. Anaconda installs PyQt5.6.0 in its root environment by default. Downgrading this package to PyQt4 will solve the problem, however, another module in my project depends on PyQt5. I want to figure out how use matplotlib with PyQt5.

According to matploblib's tutorial, it supports PyQt5 backend. I tried to run matplotlib.rcParams['backend'] = "Qt5Agg" and matplotlib.use('Qt5Agg') before the %matplotlib qt, but got same error. Am I missing something that's so obvious to others? Helps are appreciated.

F.S.
  • 1,175
  • 2
  • 14
  • 34
  • 1
    I guess for most people this works out of the box. Something is different in your case. You can check if any of `/.ipython/ipython_config.py` or `/.ipython/ipython_kernel_config.py` contains a line stating to use qt4. – ImportanceOfBeingErnest Jan 13 '18 at 22:36
  • @ImportanceOfBeingErnest thank you for your hint, I realized there are two steps needed to make it work. First, I need to use `%matplotlib qt5`. Second, I need to run `ipython profile create` in cmd to create those two files you mentioned. But the contents in these newly created files are all commented. I'm curious if you have any insight why this step is still necessary – F.S. Jan 14 '18 at 06:23
  • 2
    Agreed, it wouldn't seem necessary to create those files. Maybe you had some configuration file present beforehands that now got superseeded by the newly created and completely commented config files? Other than that I have no idea. If you think this problem might be worth documenting for others, you can leave an answer to your own question. – ImportanceOfBeingErnest Jan 14 '18 at 12:12
  • @ImportanceOfBeingErnest I'm actually setting things up on a new PC that I just built this week. The OS, even hardware is completely new.... anyway i will leave what I learned as an answer. thanks a lot – F.S. Jan 15 '18 at 01:13

2 Answers2

5

Thanks to @ImportanceOfBeingErnest (see comments below the question), I figured it out myself. Here is the answer for anyone who stumbled upon this question.

The method that should work for most people if you want to use PyQt5 in matplotlib to create figures in individual windows is to run matplotlib.use('Qt5Agg') then %matplotlib qt5.

This doesn't work for me for some reason I still don't know. But the workaround is to check the following two files: /.ipython/ipython_config.py and /.ipython/ipython_kernel_config.py and make sure there is no backend preference in it. In my case, I didn't even have these two files. The problem was solved after I created them. You can create them by running ipython profile create in cmd.

If anyone have more insights on why these two files (with no uncommented content) are needed, please let me know!

F.S.
  • 1,175
  • 2
  • 14
  • 34
-1

In my ~/.config/matplotlib/matplotlibrc file at line 41 I have the following:

backend : Qt5Agg

then I usually type

matplotlib qt in a notebook or qtconsole to get external, as opposed to inline, plots

Ken Dere
  • 1
  • 1
  • 1