Bonjour,

je souhaite pouvoir rafraîchir un graph affiché grâce à la librairie matplotLib.
Comme je galère un peu, j'ai fais ce bout de code executable.

Apres click sur le bouton up, le graph devrait ce mettre à jour, mais rien.
Je voudrais que la partie construction du graph soit dans une classe séparé (plus pratique)

J'ai essayé plein de chose, mais rien à faire.

Quelqu'un a-t-il une idée?

voici le code en question, merci d'avance pour votre aide

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/python
# -*- coding: utf-8 -*-
 
import sys
 
from lang import trad
from PyQt4 import QtGui
from PyQt4 import QtCore
 
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
from matplotlib.font_manager import FontProperties
 
 
class Main(QtGui.QMainWindow):
    '''
    Just a simple interface
    '''
 
    def __init__(self, parent=None):
        super(Main, self).__init__(parent)
 
        self.Settings = QtCore.QSettings("Yanouil", "Account")
 
    ################# Main Window #############################################
        # Data
        data = {'A': [1, 2, 1], 'B': [2, 1, 2], 'B': [2, 1, 4]}
        self.label = ['label1', 'label2', 'label3']
 
        # Make plot
        self.plot = BarPlot(self.label, data)
 
        # Button
        upButton = QtGui.QPushButton("Update")
        upButton.clicked.connect(
            lambda: self.button_clicked("up"))
 
        lay = QtGui.QVBoxLayout()
        lay.addWidget(upButton)
        lay.addWidget(self.plot)
        widget = QtGui.QWidget()
        widget.setLayout(lay)
        self.setCentralWidget(widget)
 
    def button_clicked(self, nom):
        if nom == "up":
 
            data2 = {'A': [10, 2, 1], 'B': [2, 10, 2], 'B': [2, 1, 40]}
            self.plot.updatePlot(self.label, data2)
 
 
class BarPlot(FigureCanvasQTAgg):
    '''
    Class that make the plot
    '''
 
    def __init__(self, label, data):
 
        self.label = label
        self.data = data
 
        self.fig, self.ax = plt.subplots()
 
        FigureCanvasQTAgg.__init__(self, self.fig)
 
        self.makePlot(label, data)
 
    def makePlot(self, label, data):
        N = len(label)
        ind = np.arange(N)
        width = 0.1
 
        fontP = FontProperties()
        fontP.set_size('small')
 
        c = ['b', 'g', 'r', 'c', 'm', 'y', 'b', 'g', 'r', 'c', 'm', 'y']
 
        keys = data.keys()
        i = 1
        for key in keys:
            self.ax.bar(
                ind+(i*width),
                data[key],
                width,
                alpha=0.6,
                color=c[i],
                label=key)
            i += 1
 
        self.ax.legend(loc='upper left', prop=fontP)
        self.ax.set_xticks(ind+(N*width/2))
        self.ax.set_xticklabels(label)
 
    def updatePlot(self, label, data):
        # 1 - Close the plot
        self.close() # (OK Work)
 
        # 2 - Close fig
        #self.fig.clf() # Don't Work
 
        # 3 - Close ax
        #self.ax.cla() # Don't Work
 
        # AND AFTER - Populate plot again
        self.makePlot(label, data) # Nothink happen
 
 
#############################################################################
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    main = Main()
    main.show()
    main.raise_()
    sys.exit(app.exec_())