简体   繁体   English

无法同时运行matplotlib和pyqt4

[英]Cannot run matplotlib and pyqt4 at the same time

I am running some code using PyQt4, and I would like to plot a figure using its data. 我正在使用PyQt4运行一些代码,我想使用其数据绘制图形。 But when I try to do that, it will report 但是当我尝试这样做时,它将报告

QPixmap: Must construct a QGuiApplication before a QPixmap

Below is the code: 下面是代码:

from PyQt4 import QtCore
import sys
import matplotlib.pyplot as plt
import numpy as np
def run():
   #here is some code, I delete them since they are useless for this question
   return data1 #data1 is a list with 30 elements

app = QtCore.QCoreApplication(sys.argv)
client.finished.connect(app.quit)
QtCore.QTimer().singleShot(0,lambda:client.timed_range_stream(5000))
app.exec_()
fig = plt.figure()
ax1 = fig.add_subplot(111)
data2 = run()
datalen = np.linspace(0,10,len(data2))
ax1.plot(datalen,data2,lw = 2)
plt.show()

Since the matplotlib is using pyqt4 as backend, I am so confused why this error happened. 由于matplotlib使用pyqt4作为后端,所以我很困惑为什么会发生此错误。 It should create a QGuiApplication automatically. 它应该自动创建一个QGuiApplication。 I mean whether I use pyqt4 before or not, the code below 'app.exec_()' should create a QGuiApplication automatically. 我的意思是,无论我之前是否使用pyqt4,“ app.exec_()”下面的代码都应自动创建一个QGuiApplication。 Please point out if I am wrong. 如果我错了,请指出。

Really appreciate your help! 非常感谢您的帮助! Please give me some advice. 请给我一些建议。

The complaint by PyQt is that you are not running a Gui EventLoop. PyQt抱怨说您没有运行Gui EventLoop。 app.exec_() sure starts an event loop, but that depends on what app is. app.exec_()当然会启动事件循环,但这取决于什么app In your case its QCoreApplication object. 在您的情况下,其QCoreApplication对象。 How do you expect it to start a Gui EventLoop? 您如何期望它启动Gui EventLoop? It's like buying a saucepan and expecting it to cook pizza. 这就像买一个平底锅,然后期望它会煮比萨一样。

matplotlib is based on PyQt for sure. matplotlib肯定基于PyQt I'm sure you can use it in console only applications as well. 我确定您也可以在仅控制台的应用程序中使用它。 Hence PyQt will not be able to tell if you want a gui or a console app. 因此,PyQt将无法判断您是否需要GUI或控制台应用程序。

QCoreApplication is used when you are writing a console-based application. 在编写基于控制台的应用程序时使用QCoreApplication Fewer events and processes to manage. 更少的事件和流程需要管理。 If you want to show a window, even a simple one, it takes much more work. 如果要显示一个窗口,即使是一个简单的窗口,也需要做很多工作。 And the beast to handle that extra work in QGuiApplication QGuiApplication处理额外工作的QGuiApplication

Now to the Qt version. 现在到Qt版本。 You are using PyQt4 , but the complaint says you need to create a QGuiApplication . 您正在使用PyQt4 ,但投诉显示您需要创建QGuiApplication However, there is no QGuiApplication or any reference to it in Qt4/PyQt4. 但是,在Qt4 / PyQt4中没有QGuiApplication或其任何引用。 This leads me to believe that, your matplotlib copy might be using PyQt5, or PyQt5 dependency comes in from some obscure source, I'm not sure. 这使我相信,您的matplotlib副本可能正在使用PyQt5,或者PyQt5依赖项来自某些晦涩的来源,我不确定。 Check the details of the PyQt version used. 检查所使用的PyQt版本的详细信息。

If you are using PyQt4 add from PyQt4 import QtGui in the beginning. 如果您使用的是PyQt4,请from PyQt4 import QtGui开始添加from PyQt4 import QtGui Then change the app = QtCore.QCoreApplication(...) to app = QtGui.QApplication(...) . 然后将app = QtCore.QCoreApplication(...)更改为app = QtGui.QApplication(...)

In case of PyQt5 add from PyQt5 import QtGui, QtWidgets in the beginning. 如果是PyQt5,请from PyQt5 import QtGui, QtWidgets PyQt5中添加from PyQt5 import QtGui, QtWidgets并在开头添加from PyQt5 import QtGui, QtWidgets Then change the app = QtCore.QCoreApplication(...) to app = QtWidgets.QApplication(...) . 然后将app = QtCore.QCoreApplication(...)更改为app = QtWidgets.QApplication(...)

That'll solve your problem. 这样可以解决您的问题。

PS: Remember, you cannot mix PyQt4 and PyQt5. PS:请记住,您不能混合使用PyQt4和PyQt5。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM