Wednesday, 22 May 2019

How to avoid PyCharm console crash when plotting with matplotlib?

In PyCharm, when I try to plot something using it's 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 *. I know it 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 s.r.o
  • Linux 4.15.0-50-generic
  • conda 4.6.14, with Python 3.7.3


from How to avoid PyCharm console crash when plotting with matplotlib?

No comments:

Post a Comment