导入数据和在python上单击按钮时在图形上显示它的麻烦

时间:2016-04-20 04:34:27

标签: python-2.7 pyqt4

from PyQt4.uic import loadUiType

import pandas as pd
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import (
FigureCanvasQTAgg as FigureCanvas,
NavigationToolbar2QT as NavigationToolbar)

Ui_MainWindow, QMainWindow = loadUiType('EIA_20151504_v2.ui')

上面的代码我做了以下几点。我已经导入了使用QI设计器设计的UI,并调用matplotlib后端将matplotlib greaph放入我的应用程序

class Main(QMainWindow, Ui_MainWindow):
    def __init__(self, ):
      super(Main, self).__init__()
      self.setupUi(self)

      self.RawData.clicked.connect(self.importdata)

    def addmpl(self, fig):
      self.canvas = FigureCanvas(fig)
      self.mplvl.addWidget(self.canvas)
      self.canvas.draw()
      self.toolbar = NavigationToolbar(self.canvas,
      self.MapCanvas, coordinates=True)
      self.mplvl.addWidget(self.toolbar)
      self.canvas.show()

   def importdata(self):
      choice = QtGui.QMessageBox.question(self, 'Import Data',"Do you want   to import data?", QtGui.QMessageBox.Yes |QtGui.QMessageBox.No,QtGui.QMessageBox.No)

      if choice == QtGui.QMessageBox.Yes:
          xlsfile = pd.ExcelFile('import_file.xlsx')
          self.dframe = xlsfile.parse('Sheet1', header=0, index_col=0, has_index_names=True)
          self.xList = self.dframe.columns.values
          self.xList = pd.to_datetime(self.xList)
          self.yList = self.dframe.values
          self.yList = self.yList.T
          ax1f1.plot(self.xList,self.yList)

      else:
          pass

在上面的代码中,我已经声明了一个Main类,它初始化从Qt设计器导入的UI代码,有两个函数,一个是初始化地图画布,另一个是从另一个文件导入数据,并在点击时绘制它。 RawData按钮。

但如果您尝试运行代码,则图表不会显示任何内容

if __name__ == '__main__':
  import sys
  from PyQt4 import QtGui
  import numpy as np

  fig1 = Figure()
  ax1f1 = fig1.add_subplot(111)
  app = QtGui.QApplication(sys.argv)
  main = Main()

  main.addmpl(fig1)
  main.show()
  sys.exit(app.exec_())        

请让我知道我哪里出错了

1 个答案:

答案 0 :(得分:0)

    class Main(QMainWindow, Ui_MainWindow):
        def __init__(self, ):
            super(Main, self).__init__()
            self.setupUi(self)

            self.RawData.clicked.connect(self.importdata)

        def addmpl(self, fig):
            self.canvas = FigureCanvas(fig)
            self.mplvl.addWidget(self.canvas)
            self.canvas.draw()
            self.toolbar = NavigationToolbar(self.canvas,
            self.MapCanvas, coordinates=True)
            self.mplvl.addWidget(self.toolbar)
            self.canvas.show()

       def rmmpl(self,):
            self.mplvl.removeWidget(self.canvas)
            self.canvas.close()
            self.mplvl.removeWidget(self.toolbar)
            self.toolbar.close()

       def importdata(self):
            choice = QtGui.QMessageBox.question(self, 'Import Data',"Do you want to import data?", QtGui.QMessageBox.Yes |QtGui.QMessageBox.No,QtGui.QMessageBox.No)

           if choice == QtGui.QMessageBox.Yes:
              xlsfile = pd.ExcelFile('import_file.xlsx')
              self.dframe = xlsfile.parse('Sheet1', header=0, index_col=0,   has_index_names=True)
              self.xList = self.dframe.columns.values
              self.xList = pd.to_datetime(self.xList)
              self.yList = self.dframe.values
              self.yList = self.yList.T
              ax1f1.plot(self.xList,self.yList)
              self.rmmpl()
              self.addmpl(fig1)
           else:
              pass

          def transition(self):


    if __name__ == '__main__':
       import sys
       from PyQt4 import QtGui
       import numpy as np

       fig1 = Figure()
       ax1f1 = fig1.add_subplot(111)


       app = QtGui.QApplication(sys.argv)
       main = Main()

       main.addmpl(fig1)
       main.show()
       sys.exit(app.exec_())

我纠正了一些小错误。