7

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

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
Homero Esmeraldo
  • 1,864
  • 2
  • 18
  • 34
  • `import *` [is discouraged](https://www.python.org/dev/peps/pep-0008/#imports) anyway. Besides that, you have a likely relevant warning to investigate, which you don't appear to have done. – jpmc26 May 07 '19 at 18:42
  • I have investigated, but it is very generic, so I couldn't find anything that seemed to explain how to fix it in this case. That's the only reason why I posted it in the first place. Also, the wildcard is discourage in the final code, I'm using it in the console just for debugging. I don't think the -1 is fair in this case. – Homero Esmeraldo May 07 '19 at 19:57
  • If you investigated it, you need to include what you learned about it in the question. – jpmc26 May 07 '19 at 20:02
  • Ok, I have done that now. Better? – Homero Esmeraldo May 07 '19 at 20:18
  • This seems to be pycharm specific, so it might help to state the pycharm version (and other possible settings) you're using. – ImportanceOfBeingErnest May 22 '19 at 11:06
  • @ImportanceOfBeingErnest added more specs. Thanks for the suggestion. – Homero Esmeraldo May 22 '19 at 16:12

2 Answers2

4

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.

Homero Esmeraldo
  • 1,864
  • 2
  • 18
  • 34
  • If no one finds a better solution, I will accept my own answer as the solution so far. I don't know if that is allowed in a bounty... – Homero Esmeraldo May 27 '19 at 16:46
  • 1
    https://meta.stackoverflow.com/questions/350068/can-i-answer-my-own-bounty It is possible but I don't get the reputation back. Well... I hope it is useful for others at least. :-) – Homero Esmeraldo May 27 '19 at 16:51
2

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

igrinis
  • 12,398
  • 20
  • 45
  • Thanks for the suggestion. It didn't work, though. I am already using Qt5. I run `import matplotlib; matplotlib.use('TkAgg')` and the output is `Qt5Agg`. The switch to `Tk` back didn't change the error either. :-( – Homero Esmeraldo May 27 '19 at 16:25
  • `import matplotlib; matplotlib.use('TkAgg')` should be your very first lines. Once you import `pyplot` this will not work anymore, and you can not switch backends once `pyplot` was initialized – igrinis May 28 '19 at 04:48
  • That's exactly what I did, I run `import matplotlib; matplotlib.use('TkAgg'); from matplotlib.pyplot import *` and then the rest. Each command in one line (I can't do this in a comment). I am sorry, I just realised that my previous answer was confusing. I meant: I am already using Qt5. I run `print(plt.get_backend())` and the output was `Qt5Agg`. The switch to `Tk` back end didn't change the error either. – Homero Esmeraldo May 28 '19 at 05:02
  • Try uninstalling `PyQt5` and `matplotlib`, and reinstalling them again – igrinis May 28 '19 at 10:19
  • I don't think it would work, as the problem is the same in more than one conda environment – Homero Esmeraldo May 28 '19 at 15:18
  • And it was replicated by this guy from jetbrains: https://youtrack.jetbrains.com/issue/PY-36136 – Homero Esmeraldo May 28 '19 at 20:03