IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Python Discussion :

MatplotLib : mettre à jour un graphique


Sujet :

Python

  1. #1
    Membre à l'essai
    Inscrit en
    Juin 2007
    Messages
    38
    Détails du profil
    Informations forums :
    Inscription : Juin 2007
    Messages : 38
    Points : 16
    Points
    16
    Par défaut MatplotLib : mettre à jour un graphique
    Bonjour,

    je voudrais qu'un graphique de mon interface soit mis à jour en fonction des données.
    Pour cela je créer le graphique, puis je le place dans une layout et ensuite je l'affiche.
    J'ai créé une classe qui s'occupe de cela. Cependant je n'arrive pas a faire la 'mise à jour' du graph.
    Pourtant j'essaye une méthode un peu brute, supprimer, puis recrer le graph, mais rien a faire, ca ne marche pas.

    Soit le graph s'efface et ne se re-crée pas, soit il ne change pas.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    class AccountEvoPlot(QtGui.QWidget):
        def __init__(self):
            super(AccountEvoPlot, self).__init__()
     
            # Create plot
            self.makePlot()
     
            posit = QtGui.QHBoxLayout()
            posit.addWidget(FigureCanvas(self.fig))
            self.setLayout(posit)
     
        def makeList(self):
            '''
            Construct data for the graph.
            Return 3 lists (2 are data and 1 is label)
            '''
            # Get list of the last budget (max 12)
            aId = tools.accountId()
            lastBudget = tools.getLastBudgetId()
            budgetList = tools.getBudgetIdList(lastBudget)
            if len(budgetList) > 12:
                budgetList = budgetList[0:12]
            label = []
            listR = []
            listRT = []
            r = 0.0
            c = 0.0
            w = 0.0
            for budgetId in budgetList:
                realSum = calculation.realBudgetSum(budgetId, aId)
                r += realSum
                checkSum = calculation.checkBudgetSum(budgetId, aId)
                c += checkSum
                waitSum = calculation.waitBudgetSum(budgetId, aId)
                w += waitSum
                year = tools.returnVal('Budgets', 'year', budgetId).toString()
                month = tools.returnVal('Budgets', 'mounth', budgetId).toString()
                name = str(month) + '/' + str(year)
                listRT.append(r)
                listR.append(realSum)
                label.append(name)
     
            if len(label) <= 12:
                toAdd = 12 - len(label)
                for i in range(0, toAdd):
                    label.append('')
                    listR.append(0.0)
                    listRT.append(0.0)
     
            return label, listR, listRT
     
        def makePlot(self):
            '''
            Construc the plot
            '''
            # Get data
            label, listR, listRT = self.makeList()
     
            # Give plot's parameters
            N = 12
            ind = np.arange(N)  # the x locations for the groups
            width = 0.35       # the width of the bars
     
            title = tools.returnVal(
                'Accounts', 'info', tools.accountId()).toString()
     
            # Construct the plot
            self.fig, ax = plt.subplots()
     
            # Setup the plot
            ax.set_ylabel(title)
            ax.set_xticks(ind + width)
            ax.set_xticklabels(label)
            ax.bar(ind, listR, width, color='r')
            ax.bar(ind+width, listRT, width, color='y')
     
        def updatePlot(self):
            '''
            UpDate data and plot
            '''
            # Destroy plot
            plt.cla() #or plt.clf, that the same... Don't work
            # Make new plot
            self.makePlot()
            # Print data to be sure that's work
            print 'tools.accountId()', tools.accountId(), 'tools.budgetId()', tools.budgetId()
            label, listR, listRT = self.makeList()
            print label
            print listR
    Pour l'utiliser, j'appel plot = AccountEvoPlot()
    Pour le mettre à jour plot.updatePlot()

    Mais ca ne marche pas. Des idées et/ou conseils?

  2. #2
    Membre à l'essai
    Inscrit en
    Juin 2007
    Messages
    38
    Détails du profil
    Informations forums :
    Inscription : Juin 2007
    Messages : 38
    Points : 16
    Points
    16
    Par défaut
    Je m'en sort en supprimant directement le widget, mais je ne suis pas sure que cela soit très optimisé niveau mémoire.
    Ca m'a l'air assez sale comme façon de faire.

    voilà le code qui marche, des conseils???

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    class AccountEvoPlot(QtGui.QWidget):
        def __init__(self):
            super(AccountEvoPlot, self).__init__()
     
            # Create plot
            self.plot = FigureCanvas(self.makePlot())
     
            self.posit = QtGui.QHBoxLayout()
            self.posit.addWidget(self.plot)
            self.setLayout(self.posit)
     
        def makeList(self):
            '''
            Construct data for the graph.
            Return 3 lists (2 are data and 1 is label)
            '''
            # Get list of the last budget (max 12)
            aId = tools.accountId()
            lastBudget = tools.getLastBudgetId()
            budgetList = tools.getBudgetIdList(lastBudget)
            if len(budgetList) > 12:
                budgetList = budgetList[0:12]
            label = []
            listR = []
            listRT = []
            r = 0.0
            c = 0.0
            w = 0.0
            for budgetId in budgetList:
                realSum = calculation.realBudgetSum(budgetId, aId)
                r += realSum
                checkSum = calculation.checkBudgetSum(budgetId, aId)
                c += checkSum
                waitSum = calculation.waitBudgetSum(budgetId, aId)
                w += waitSum
                year = tools.returnVal('Budgets', 'year', budgetId).toString()
                month = tools.returnVal('Budgets', 'mounth', budgetId).toString()
                name = str(month) + '/' + str(year)
                listRT.append(r)
                listR.append(realSum)
                label.append(name)
     
            if len(label) <= 12:
                toAdd = 12 - len(label)
                for i in range(0, toAdd):
                    label.append('')
                    listR.append(0.0)
                    listRT.append(0.0)
     
            return label, listR, listRT
     
        def makePlot(self):
            '''
            Construc the plot
            '''
            # Get data
            label, listR, listRT = self.makeList()
     
            # Give plot's parameters
            N = 12
            ind = np.arange(N)  # the x locations for the groups
            width = 0.35       # the width of the bars
     
            title = tools.returnVal(
                'Accounts', 'info', tools.accountId()).toString()
     
            # Construct the plot
            fig, ax = plt.subplots()
     
            # Setup the plot
            ax.set_ylabel(title)
            ax.set_xticks(ind + width)
            ax.set_xticklabels(label)
            ax.bar(ind, listR, width, color='r')
            ax.bar(ind+width, listRT, width, color='y')
     
            return fig
     
        def updatePlot(self):
            '''
            UpDate data and plot
            '''
            # Destroy widget
            self.plot.deleteLater()
            # Make new widget
            self.plot = FigureCanvas(self.makePlot())
            self.posit.addWidget(self.plot)

Discussions similaires

  1. [XL-2007] Mettre à jour légende graphique dynamique
    Par thebest31 dans le forum Excel
    Réponses: 2
    Dernier message: 30/01/2013, 15h30
  2. [E-03] Mettre à jour l'étiquette d'un graphique
    Par moilou2 dans le forum Excel
    Réponses: 2
    Dernier message: 30/01/2009, 11h18
  3. comment mettre à jour un sousformulaire-graphique?
    Par carlostropico dans le forum IHM
    Réponses: 2
    Dernier message: 14/10/2008, 12h01
  4. Mettre a jour un graphique sur Excel
    Par LaPanic dans le forum Excel
    Réponses: 20
    Dernier message: 19/02/2008, 13h23
  5. Mettre à jour les liens des graphiques d'Excel dans Powerpoint
    Par illight dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 18/06/2007, 15h17

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo