[英]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: 在PyCharm中,当我尝试使用其交互式控制台绘制内容时,例如:
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. 当我在终端的ipython控制台中运行类似的东西时它工作正常,错误只发生在Pycharm中。
On the other hand, if import matplotlib with import matplotlib.pyplot as plt
it works fine: 另一方面,如果导入matplotlib
import matplotlib.pyplot as plt
它工作正常:
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
): 但是,如果我同时执行这两项操作,它也会崩溃(甚至使用
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. 所以它与使用matplotlib库以及使用*导入以及在第一次导入后在交互式控制台中运行相关。 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. https://github.com/matplotlib/matplotlib/issues/13296但我的情况似乎与多处理无关。 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;
即使pycharm在幕后做某事,我也想知道它为什么会改变,因为我在一个月前没有遇到任何问题;
Suppress warning "QApplication was not created in main() thread" and other posts related to C++, which is not my case; 抑制警告“QApplication未在main()线程中创建”以及与C ++相关的其他帖子,这不是我的情况;
WARNING: QApplication was not created in main() thread -> related to pycharm, but has an additional error different than mine 警告:QApplication不是在main()线程中创建的 - >与pycharm相关,但是有一个不同于我的额外错误
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 我把这个问题发给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 .
Python Scientific 。
This worked for me, although it doesn't plot in the PyCharm window. 这对我有用,虽然它没有在PyCharm窗口中绘图。
There several things you can try: 您可以尝试以下几种方法:
First, you can try to update the Qt. 首先,您可以尝试更新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. 如果您使用的是
Qt4
,请尝试使用Qt5
后端。
Next, update Qt5
to the latest version via 接下来,通过更新
Qt5
到最新版本
pip install --upgrade PyQt5
Also, you can try ditching Qt
and switch to Tk
back end: add 此外,您可以尝试放弃
Qt
并切换到Tk
后端:添加
import matplotlib
matplotlib.use('TkAgg')
before importing pyplot
在导入
pyplot
之前
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.