Partage
  • Partager sur Facebook
  • Partager sur Twitter

[Python + PyQt] Je n'arrive pas à tracer des valeurs

Sujet résolu
    2 décembre 2011 à 15:32:36

    Hello everyone, I'm trying to plot live data extracting from remote
    devices (here it's simulated by get_info1 and 2 the result is always
    0.8 or 0.9
    Bonjour à tous. J'essaye de plotter des données en live sur un graphe (ici simulé par get_info1 et 2 qui retournent 0.8 et 0.9
    A la fin de de ma boucle while le dessin n'apparait pourtant pas à l'écran alors que tout le reste est bien exécuté.

    Avez-vous une idée?

    Merci d'avance.


    #!/usr/bin/env python
    
    from visa import *
    from pylab import *
    import sys
    from PyQt4 import QtGui
    import numpy as np
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
    from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg
    as NavigationToolbar
    
    #===============================================================================
    #
    #===============================================================================
    
    class CPUMonitor(FigureCanvas):
       """Matplotlib Figure widget"""
       def __init__(self,parent):
    
           # first image setup
           self.fig = Figure()
           self.ax = self.fig.add_subplot(111)
           # initialization of the canvas
           FigureCanvas.__init__(self, self.fig)
           # set specific limits for X and Y axes
           self.ax.set_xlim(0, 2)
           self.ax.set_ylim(0, 1.5)
    
    
           # generates first "empty" plots
           self.user, self.nice = [], []
           self.l_user, = self.ax.plot([], self.user, label='Voltage')
           self.l_nice, = self.ax.plot([], self.nice, label='Voltage2')
    
    
    
           self.ax.legend()
    
           # force a redraw of the Figure
           self.fig.canvas.draw()
    
           self.principal()
    
    
    
    
       def principal(self) :
           stop = 0
           while stop==0 :
               time.sleep(1)
               self.set_voltage()
               time.sleep(1)
               result1 = self.get_info()
               result2 = self.get_info2()
    
    #          append new data to the datasets
               self.user.append(result1)
               self.nice.append(result2)
    
           self.l_user.set_data(range(len(self.user)), self.user)
           self.l_nice.set_data(range(len(self.nice)), self.nice)
    
           # force a redraw of the Figure
    
           self.fig.canvas.draw()
           FigureCanvas.updateGeometry(self)
    
       def get_info(self):
           return [0.8]
    
       def get_info2(self) :
           return [0.9]
    
       def set_voltage(self) :
           print "ok"
    
    
    
    
    #===============================================================================
    #
    #===============================================================================
    
    
    
    class ApplicationWindow(QtGui.QMainWindow):
       """Example main window"""
       def __init__(self):
           # initialization of Qt MainWindow widget
           QtGui.QMainWindow.__init__(self)
           # set window title
           self.setWindowTitle("QHE manip")
           # instantiate a widget, it will be the main one
           self.main_widget = QtGui.QWidget(self)
           # create a vertical box layout widget
           vbl = QtGui.QVBoxLayout(self.main_widget)
           # instantiate our Matplotlib canvas widget
           qmc = CPUMonitor(self.main_widget)
           # instantiate the navigation toolbar
           ntb = NavigationToolbar(qmc, self.main_widget)
           # pack these widget into the vertical box
           vbl.addWidget(qmc)
           vbl.addWidget(ntb)
    
           # set the focus on the main widget
           self.main_widget.setFocus()
           # set the central widget of MainWindow to main_widget
           self.setCentralWidget(self.main_widget)
    
    # create the GUI application
    qApp = QtGui.QApplication(sys.argv)
    # instantiate the ApplicationWindow widget
    aw = ApplicationWindow()
    # show the widget
    aw.show()
    # start the Qt main loop execution, exiting from this script
    # with the same return code of Qt application
    sys.exit(qApp.exec_())
    
    • Partager sur Facebook
    • Partager sur Twitter
      3 décembre 2011 à 8:49:26

      Bonjour,

      Dans ta méthode "principal", tu ne stop jamais ta boucle?
      Si c'est le cas, tu n'en sortiras jamais et tu ne pourras appeler draw()

      J'avais un probleme similaire ou je devais dessiner en "live" un graphique, j'ai été obligé de faire les calculs dans un QThread et émettre tout les x sec un signal pour demander de redessiner le graph.
      • Partager sur Facebook
      • Partager sur Twitter
        5 décembre 2011 à 15:41:50

        Salut,

        Merci baryum! J'ai changé un peu le code et le problème persiste! Ce que je ne comprend pas c'est qu'avec le code suivant cela marche alors que j'ai l'impression que la seule chose que je fais c'est remplécé l'appel du timer par ma def principal.

        # -*- coding: utf-8 -*-
        """
        Created on Fri Dec 02 17:10:22 2011
        
        @author: lafont
        """
        
        #!/usr/bin/env python
        
        from visa import *
        from pylab import *
        import sys
        from PyQt4 import QtGui
        import numpy as np
        from matplotlib.figure import Figure
        from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
        from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
        
        #===============================================================================
        # 
        #===============================================================================
        
        class CPUMonitor(FigureCanvas):
            """Matplotlib Figure widget"""
            def __init__(self,parent):
        
                # first image setup
                self.fig = Figure()
                self.ax = self.fig.add_subplot(111)   
                # initialization of the canvas
                FigureCanvas.__init__(self, self.fig)       
                # set specific limits for X and Y axes
        #        self.ax.set_xlim(0, 40)
        #        self.ax.set_ylim(0, 1.5)
        
                
        #       
                FigureCanvas.updateGeometry(self)
                # generates first "empty" plots
                self.user, self.nice = [], []
                self.l_user, = self.ax.plot([], self.user, label='Voltage')
                self.l_nice, = self.ax.plot([], self.nice, label='Voltage2')
                
                
                # add legend to plot
                self.ax.legend()
        
                # force a redraw of the Figure
                self.fig.canvas.draw()
                
             
                
                
                # start the timer, to trigger an event every x milliseconds)
                self.timer = self.startTimer(1000)
                self.timerEvent(None)
               
                       
            def get_info(self):       
                return [0.8]
        	 
            def get_info2(self) :
                return [0.9]
        
            def set_voltage(self) :      
                print "ok"
                
                
            
            
            
            
        #        
        
            def timerEvent(self, evt):
                """Custom timerEvent code, called upon timer event receive""" 
                result1 = self.get_info()
                result2 = self.get_info2()        
            
        #          append new data to the datasets
                self.user.append(result1)
                self.nice.append(result2)
                # update lines data using the lists with new data
                self.l_user.set_data(range(len(self.user)), self.user)
                self.l_nice.set_data(range(len(self.nice)), self.nice)
        	
                # force a redraw of the Figure
                self.fig.canvas.draw()
                FigureCanvas.updateGeometry(self)
        
        
        
        #===============================================================================
        #         
        #===============================================================================
                
        
        
        class ApplicationWindow(QtGui.QMainWindow):
            """Example main window"""
            def __init__(self):
                # initialization of Qt MainWindow widget
                QtGui.QMainWindow.__init__(self)
                # set window title
                self.setWindowTitle("QHE manip")
                # instantiate a widget, it will be the main one
                self.main_widget = QtGui.QWidget(self)
                # create a vertical box layout widget
                vbl = QtGui.QVBoxLayout(self.main_widget)
               
                # instantiate our Matplotlib canvas widget
                qmc = CPUMonitor(self.main_widget)
                # instantiate the navigation toolbar
                ntb = NavigationToolbar(qmc, self.main_widget)
                # pack these widget into the vertical box
                vbl.addWidget(qmc)
                vbl.addWidget(ntb)
        
                # set the focus on the main widget
                self.main_widget.setFocus()
                # set the central widget of MainWindow to main_widget
                self.setCentralWidget(self.main_widget)
        
        # create the GUI application
        qApp = QtGui.QApplication(sys.argv)
        # instantiate the ApplicationWindow widget
        aw = ApplicationWindow()
        # show the widget
        aw.show()
        # start the Qt main loop execution, exiting from this script
        # with the same return code of Qt application
        sys.exit(qApp.exec_())
        


        Et voici le prog qui ne veux rien afficher:

        #!/usr/bin/env python
        
        from visa import *
        from pylab import *
        import sys
        from PyQt4 import QtGui
        import numpy as np
        from matplotlib.figure import Figure
        from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
        from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
        
        #===============================================================================
        # 
        #===============================================================================
        
        class CPUMonitor(FigureCanvas):
            """Matplotlib Figure widget"""
            def __init__(self,parent):
        
                # first image setup
                self.fig = Figure()
                self.ax = self.fig.add_subplot(111)   
                # initialization of the canvas
                FigureCanvas.__init__(self, self.fig)       
                # set specific limits for X and Y axes
                self.ax.set_xlim(0, 2)
                self.ax.set_ylim(0, 1.5)
        
                # and disable figure-wide autoscale 
                self.ax.set_autoscale_on(False)
        
                # generates first "empty" plots
                self.user, self.nice = [], []
                self.l_user, = self.ax.plot([], self.user, label='Voltage')
                self.l_nice, = self.ax.plot([], self.nice, label='Voltage2')
                
                
                # add legend to plot
                self.ax.legend()
        
                # force a redraw of the Figure
                self.fig.canvas.draw()
                
               
        
                self.principal()
               
        #===============================================================================
        #  
        #===============================================================================
                
            def principal(self) :    
               
                stop = 0
                while stop == 0 :
                    time.sleep(1)
                    self.set_voltage()
                    time.sleep(1)
                    result1 = self.get_info()
                    result2 = self.get_info2()        
                    
        #          append new data to the datasets
                    self.user.append(result1)
                    self.nice.append(result2)
                    self.fig.canvas.draw()
                    self.l_user.set_data(range(len(self.user)), self.user)
                    self.l_nice.set_data(range(len(self.nice)), self.nice)
                
                    
        	
                    FigureCanvas.updateGeometry(self)    
                       
            def get_info(self):
                print "get info 1"
                return [0.8]
        	 
            def get_info2(self) :
                print "get info 2"
                return [0.9]
        
            def set_voltage(self) :
                print "set voltage"
                
        #===============================================================================
        #         
        #===============================================================================
                
        
        
        class ApplicationWindow(QtGui.QMainWindow):
            """Example main window"""
            def __init__(self):
                # initialization of Qt MainWindow widget
                QtGui.QMainWindow.__init__(self)
                # set window title
                self.setWindowTitle("QHE manip")
                # instantiate a widget, it will be the main one
                self.main_widget = QtGui.QWidget(self)
                # create a vertical box layout widget
                vbl = QtGui.QVBoxLayout(self.main_widget)
                # instantiate our Matplotlib canvas widget
                qmc = CPUMonitor(self.main_widget)
                # instantiate the navigation toolbar
                ntb = NavigationToolbar(qmc, self.main_widget)
                # pack these widget into the vertical box
                vbl.addWidget(qmc)
                vbl.addWidget(ntb)
                # set the focus on the main widget
                self.main_widget.setFocus()
                # set the central widget of MainWindow to main_widget
                self.setCentralWidget(self.main_widget)
        
        # create the GUI application
        qApp = QtGui.QApplication(sys.argv)
        # instantiate the ApplicationWindow widget
        aw = ApplicationWindow()
        # show the widget
        aw.show()
        # start the Qt main loop execution, exiting from this script
        # with the same return code of Qt application
        sys.exit(qApp.exec_())
        
        • Partager sur Facebook
        • Partager sur Twitter

        [Python + PyQt] Je n'arrive pas à tracer des valeurs

        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
        × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
        • Editeur
        • Markdown