简体   繁体   中英

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.

错误的屏幕截图

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

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 sro
  • Linux 4.15.0-50-generic
  • conda 4.6.14, with Python 3.7.3
  • Qt5

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM