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
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With