Bonjour à tous. J'éssaye d'écrire un programme pouvant afficher en live l'évolution d'une variable (chez moi la tension lue par un appareil de mesure). J'y arrive à merveille sauf que je n'arrive pas à faire évoluer dynamiquement la taille de la fenêtre afin de voir tout mon graphe.
La partie utilisant les appareils de mesure "get_cpu_usage" peut être remplacé par n'importe quel vecteur, aléatoire par exemple.
Même en changeant avec dans la barre du graphe et en mettant tout en autoscale mon graphe "sort de la fenêtre".
N'hésitez pas à faire des commentaires annexes sur le code. Je suis débutant!
Merci d'avance
Voici mon code:
#!/usr/bin/env python
from visa import *
from pylab import *
# for command-line arguments
import sys
# Python Qt4 bindings for GUI objects
from PyQt4 import QtGui
# Numpy functions for image creation
import numpy as np
# Matplotlib Figure object
from matplotlib.figure import Figure
# import the Qt4Agg FigureCanvas object, that binds Figure to
# Qt4Agg backend. It also inherits from QWidget
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
# import the NavigationToolbar Qt4Agg widget
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
class CPUMonitor(FigureCanvas):
"""Matplotlib Figure widget to display CPU utilization"""
def __init__(self,parent):
## # save the current CPU information (used by updating algorithm)
## self.before = self.prepare_cpu_usage()
# 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, 1000)
# self.ax.set_ylim(0, 0.0025)
# and disable figure-wide autoscale
self.ax.set_autoscale_on(True)
# generates first "empty" plots
self.user = []
self.l_user, = self.ax.plot([], self.user, label='Voltage')
# add legend to plot
self.ax.legend()
# force a redraw of the Figure
self.fig.canvas.draw()
# initialize the iteration counter
self.cnt = 0
# call the update method (to speed-up visualization)
self.timerEvent(None)
# start the timer, to trigger an event every second (= 1000 millisecs)
self.timer = self.startTimer(100)
## def prepare_cpu_usage(self):
## """helper function to return CPU usage info"""
## # get the CPU times using psutil module
## t = p.cpu_times()
## # return only the values we're interested in
## return [t.user, t.nice, t.system, t.idle]
def get_cpu_usage(self):
my_instrument = instrument("GPIB0::24", term_chars = CR)
my_instrument.write("PRESET NORM")
my_instrument.write("DCV ")
my_instrument.write("TRIG AUTO")
mesure = my_instrument.read_values()
fichier = open("fichier.dat", "a")
fichier.write(str(mesure))
fichier.close()
return [mesure]
def timerEvent(self, evt):
"""Custom timerEvent code, called upon timer event receive"""
## # get the cpu percentage usage
result = self.get_cpu_usage()
# append new data to the datasets
self.user.append(result)
# update lines data using the lists with new data
self.l_user.set_data(range(len(self.user)), self.user)
# 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("Matplotlib Figure in a Qt4 Window With NavigationToolbar")
# 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_())
× 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.