在PyQt4中嵌入matplotlib的问题

时间:2017-11-27 03:26:35

标签: python python-2.7 matplotlib pyqt pyqt4

以下是代码:

from matplotlib.backends.qt_compat import QtCore, QtGui
from matplotlib.backends.backend_qt4agg import (FigureCanvasQTAgg as FigureCanvas)
from matplotlib.figure import Figure
import sys

class MyCanvas(FigureCanvas):
    def __init__(self, parent=None, key="case 1"):

        self.case_dict = {"case 1": [2, 4, 6, 8, 10],
                        "case 2": [3, 4, 5, 6, 7]}

        self.key = key

        self.fig = Figure()
        self.axes = self.fig.add_subplot(111)

        self.compute_initial_figure()

        FigureCanvas.__init__(self, self.fig)
        self.setParent(parent)

        FigureCanvas.updateGeometry(self)

    def compute_initial_figure(self):
        x = [1, 2, 3, 4, 5]
        y = self.case_dict[self.key]
        self.axes.set_title("%s" % self.key)
        self.axes.scatter(x, y)

class MainWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.canvas_widget = MyCanvas()

        self.combo = QtGui.QComboBox()
        for key in MyCanvas().case_dict:
            self.combo.addItem(key)
            self.combo.setCurrentIndex(-1)

        self.combo.currentIndexChanged.connect(self.showPlot)

        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.canvas_widget)
        layout.addWidget(self.combo)

    def showPlot(self):
        key_name = self.combo.currentText()
        self.canvas_widget = MyCanvas(key=unicode(key_name))

qApp = QtGui.QApplication(sys.argv)

aw = MainWindow()
aw.show()
sys.exit(qApp.exec_())

我希望在更改QComboBox中的Item时刷新图形。但现在,没有任何反应。 when choose 'case 1' when choose 'case 2'

我是Python的初学者,只是不知道如何解决这个问题。那么,我该怎么做才能将QComboBox与matplotlib生成的数字联系起来?

1 个答案:

答案 0 :(得分:1)

使用指令self.canvas_widget = MyCanvas(key=unicode(key_name)),您正在创建一个没有父级的新窗口小部件,因此不会显示,除了建议不要创建新的窗口小部件,因为它不需要内存费用,您必须使用为它重用小部件我编写了setKey方法,该方法在内部调用compute_initial_figure方法,该方法负责重绘重用分散。

from matplotlib.backends.qt_compat import QtCore, QtGui
from matplotlib.backends.backend_qt4agg import (FigureCanvasQTAgg as FigureCanvas)
from matplotlib.figure import Figure
import sys

class MyCanvas(FigureCanvas):
    def __init__(self, parent=None, key="case 1"):

        self.case_dict = {"case 1": [2, 4, 6, 8, 10],
                        "case 2": [3, 4, 5, 6, 7]}
        self.key = key
        self.fig = Figure()
        self.axes = self.fig.add_subplot(111)
        FigureCanvas.__init__(self, self.fig)
        self.setParent(parent)
        FigureCanvas.updateGeometry(self)

        self.sc = None
        self.compute_initial_figure()

    def setKey(self, key):
        self.key = key
        self.compute_initial_figure()

    def compute_initial_figure(self):
        x = [1, 2, 3, 4, 5]
        y = self.case_dict[self.key]
        self.axes.set_title("%s" % self.key)
        if self.sc is None:
            self.sc = self.axes.scatter(x, y)
        else:
            import numpy as np
            data = np.stack((np.asarray(x), np.asarray(y)), axis=1)
            self.sc.set_offsets(data)
            self.draw()


class MainWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.canvas_widget = MyCanvas()

        self.combo = QtGui.QComboBox()
        for key in MyCanvas().case_dict:
            self.combo.addItem(key)
            self.combo.setCurrentIndex(-1)

        self.combo.currentIndexChanged.connect(self.showPlot)

        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.canvas_widget)
        layout.addWidget(self.combo)

    def showPlot(self):
        key_name = self.combo.currentText()
        self.canvas_widget.setKey(unicode(key_name))