Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid PyCharm console crash "WARNING: QApplication was not created in the main() thread" when plotting with matplotlib?

In PyCharm, when I try to plot something using its interactive console, such as:

In[2]: from matplotlib.pyplot import *
In[3]: x = range(5)
In[4]: y = range(5,10)
In[5]: plot(x,y)
WARNING: QApplication was not created in the main() thread.
Out[5]: [<matplotlib.lines.Line2D at 0x7fade916a438>]
In[6]: show()

It opens a window and crashes. I have to stop the console and start a new one.

screenshot of the error

It works fine when I run anything like that in an ipython console in my terminal, the error happens only in Pycharm, it seems.

On the other hand, if import matplotlib with import matplotlib.pyplot as plt it works fine:

In[2]: import matplotlib.pyplot as plt
In[3]: x = range(5)
In[4]: y = range(5,10)
In[5]: plt.plot(x,y)
Out[5]: [<matplotlib.lines.Line2D at 0x7fd3453b72e8>]
In[6]: plt.show()

But if I do both, it crashes too (even calling the plot function using plt.plot):

In[2]: from matplotlib.pyplot import *
In[3]: import matplotlib.pyplot as plt
In[4]: x = range(5)
In[5]: y = range(5,10)
In[6]: plt.plot(x,y)
WARNING: QApplication was not created in the main() thread.
Out[6]: [<matplotlib.lines.Line2D at 0x7fade916a438>]
In[7]: plt.show()

Furthermore, when I run it all in one command, it works the first time. But if I try to plot another time, it crashes:

In[2]: from matplotlib.pyplot import *
  ...: x = range(5)
  ...: y = range(5,10)
  ...: plot(x,y)
  ...: show()
In[3]: plot(x,y)
WARNING: QApplication was not created in the main() thread.
Out[3]: [<matplotlib.lines.Line2D at 0x7fc68a3009e8>]
In[4]: show()

So it is something related with using the matplotlib library with the import using * and with running in the interactive console after the first time it was imported. I know the wildcard import is not recommended, but sometimes it is useful to do it for a sake of testing things faster and being less verbose.

Looking for this warning online, I have only found these

  • https://github.com/matplotlib/matplotlib/issues/13296 But my case doesn't seem to be related to multiprocessing. And even if pycharm is doing something behind the scenes, I wonder why it has changed, as I had no problems with this like a month ago;

  • Suppress warning "QApplication was not created in main() thread" and other posts related to C++, which is not my case;

  • WARNING: QApplication was not created in main() thread -> related to pycharm, but has an additional error different than mine

Which didn't help much. Anyone knows what is happening and how to solve it?

SPECS:

  • PyCharm 2019.1.2 (Professional Edition)
  • Build #PY-191.7141.48, built on May 7, 2019
  • JRE: 11.0.2+9-b159.56 amd64
  • JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
  • Linux 4.15.0-50-generic
  • conda 4.6.14, with Python 3.7.3
  • Qt5
like image 646
Homero Esmeraldo Avatar asked May 04 '19 00:05

Homero Esmeraldo


2 Answers

I sent this question to JetBrains: https://youtrack.jetbrains.com/issue/PY-36136

They couldn't find a solution yet, but the workaround they suggested is the following:

Disable Show plots in tool window in File | Settings | Tools | Python Scientific.

This worked for me, although it doesn't plot in the PyCharm window.

like image 156
Homero Esmeraldo Avatar answered Nov 10 '22 12:11

Homero Esmeraldo


There several things you can try:

First, you can try to update the Qt. You may have some older version. Run

print(plt.get_backend())

to verify which backend you are using. If you are using Qt4, try Qt5 back end.

Next, update Qt5 to the latest version via

pip install --upgrade PyQt5

Also, you can try ditching Qt and switch to Tk back end: add

import matplotlib
matplotlib.use('TkAgg')

before importing pyplot

like image 34
igrinis Avatar answered Nov 10 '22 12:11

igrinis