簡體   English   中英

Python:pyQt、Qthread 和 matplotlib:子圖在第二次調用時崩潰

[英]Python: pyQt, Qthread and matplotlib: subplots crash at the second call

我正在嘗試使用 matplotlib 繪制帶有線程結果的圖形。 首先,我使用 pyqt 按鈕啟動線程,一切正常。 但是第二次按下按鈕時,子圖崩潰了,因為我認為元組無法修改。 這是我的簡化代碼,您可以自己嘗試查看:

 from PyQt4 import QtGui, QtCore import matplotlib.pyplot as plt import numpy as np from PyQt4.Qt import * import sys class thread(QThread): def __init__(self, parent=None): QThread.__init__(self, parent) def __del__(self): self.wait() def render(self): self.start() def run(self): data = [(1, 10), (3, 100), (4, 1000), (5, 100000)] data_in_array = np.array(data) transposed = data_in_array.T x, y = transposed fig, ax = plt.subplots(1,1) ax.plot(x, y, 'ro') ax.plot(x, y, 'b-') plt.show() class Window(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) self.button = QtGui.QPushButton('Test', self) self.button.clicked.connect(self.handleButton) layout = QtGui.QVBoxLayout(self) layout.addWidget(self.button) self.thread=thread() def handleButton(self): self.thread.render() if __name__ == '__main__': app = QtGui.QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())

請問,我該如何解決它以避免崩潰並多次使用我的按鈕? 謝謝

這不是混合 Qt 應用程序和 Matplotlib(在其后端使用 Qt)的正確方法。

請參閱https://matplotlib.org/gallery/user_interfaces/embedding_in_qt_sgskip.html以獲得比我能提供的更好的解釋。

我遇到了同樣的問題,正在尋找解決方案。

通過添加這 3 行解決了這個問題。 請在導入 pyplot 之前添加這 3 行

import matplotlib as mpl
mpl.rcParams['backend'] = "qt4agg"
mpl.rcParams['backend.qt4'] = "PySide"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM