简体   繁体   English

如何避免PyCharm控制台崩溃“警告:在使用matplotlib进行绘图时,未在main()线程中创建QApplication?

[英]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 在网上寻找这个警告,我只发现了这些

Which didn't help much. 哪个没有多大帮助。 Anyone knows what is happening and how to solve it? 任何人都知道发生了什么以及如何解决它?

SPECS: 眼镜:

  • PyCharm 2019.1.2 (Professional Edition) PyCharm 2019.1.2(专业版)
  • Build #PY-191.7141.48, built on May 7, 2019 Build#PY-191.7141.48,建于2019年5月7日
  • JRE: 11.0.2+9-b159.56 amd64 JRE:11.0.2 + 9-b159.56 amd64
  • JVM: OpenJDK 64-Bit Server VM by JetBrains sro JVM:JetBrains sro的OpenJDK 64位服务器VM
  • Linux 4.15.0-50-generic Linux 4.15.0-50-通用
  • conda 4.6.14, with Python 3.7.3 conda 4.6.14,Python 3.7.3
  • Qt5 QT5

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.

相关问题 通过 matplotlib 在 Pycharm 调试控制台中进行交互式绘图 - Interactive plotting in Pycharm debug console through matplotlib 该代码在Windows上有效,但在Linux上显示错误“未在主线程中创建QApplication”,如何解决, - this code works on windows but giving error on Linux “QApplication was not created in main thread” ,how to resolve it, 在 main() 中创建 QApplication 子类时 PySide2 崩溃 - PySide2 crash when creating QApplication subclass within main() 使用 Python 和 matplotlib 在循环中绘图时如何避免过度使用 memory - How to avoid excessive memory usage when plotting in a loop with Python and matplotlib 在Django中使用QT刮取一次,在main()线程中未创建使用QApplication的下一次运行时崩溃 - Scraping with QT in Django works once, crashes on next run with QApplication was not created in the main() thread python matplotlib:在非主线程中绘制到 GUI - python matplotlib: Plotting to GUI in non-main thread 在使用py2app构建PySide应用程序包时,QApplication没有在主线程中运行 - QApplication is not running in main thread when building PySide app bundle with py2app 使用 typing.NewType 时避免 PyCharm 类型警告 - Avoid PyCharm Type-Warning when using typing.NewType 在 matplotlib 中绘制分类数据时避免将字符串解释为日期 - Avoid interpreting strings as dates when plotting categorical data in matplotlib 使用 matplotlib 绘制图形时出现间歇性错误“RuntimeError: main thread is not in main loop” - Intermittent error while plotting graph using matplotlib “RuntimeError: main thread is not in main loop”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM