在PyQt GUI中嵌入Matplotlib图表

时间:2014-03-19 15:00:12

标签: python matplotlib pyqt

我编写了以下脚本,该脚本使用一个调用Matplotlib图表的按钮创建一个空GUI:

import sys
import os
from PyQt4 import QtGui
from PyQt4 import *
import matplotlib.pyplot as plt

class SmallGUI(QtGui.QMainWindow):
    def __init__(self):
        super(SmallGUI,self).__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(1010,800,1010,800)
        self.setWindowTitle('Sample')

        palette = QtGui.QPalette()
        palette.setColor(QtGui.QPalette.Background,QtCore.Qt.white)
        self.setPalette(palette)

        #Chart button
        self.MyButton = QtGui.QPushButton(self)
        self.MyButton.setGeometry(QtCore.QRect(88,65,110,20))
        self.MyButton.setText('Create chart')
        ###############

        QtCore.QObject.connect(self.MyButton,QtCore.SIGNAL("clicked(bool)"),self.makeChart)

        self.show()

    def makeChart(self):
        plt.plot([1,2,3,4])
        plt.ylabel('some numbers')
        plt.show()

def main():
    app = QtGui.QApplication(sys.argv)
    sampleForm = SmallGUI()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

我想知道是否有任何方法可以将图表嵌入到GUI中。换句话说,是否可以使图表显示附加到用户界面而不是弹出新的TK窗口?

0 个答案:

没有答案