Hello everyone,
I've adapted a python code to plot real time data but I don't manage
to have an auto-scale on my graph. I don't understand because I use"
set_autoscale_on(True)"
Do you have an idea?
Here is "my" simplified 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):
# first image setup
self.fig = Figure()
self.ax = self.fig.add_subplot(111)
# self.fig.apply__adjustable()
#
# 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(True)
#self.ax.set_adjustable('datalim')
# 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()
# 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 x milliseconds)
self.timer = self.startTimer(100)
def get_info(self):
# my_instrument1 = instrument("GPIB0::24", term_chars = CR)
# my_instrument1.write("DCV ")
# my_instrument1.write("TRIG AUTO")
# mesure1 = my_instrument1.read_values()
fichier = open("fichier.dat", "a")
fichier.write(str(1)+"\t")
fichier.close()
fichier = open("fichier.dat", "a")
fichier.write(str(1)+"\t")
fichier.close()
return [0.8]
def get_info2(self):
# my_instrument2 = instrument("GPIB0::??", term_chars = CR)
# my_instrument2.write("DCV ")
# my_instrument2.write("TRIG AUTO")
# mesure2 = my_instrument2.read_values()
#
fichier = open("fichier.dat", "a")
fichier.write(str(2)+"\n")
fichier.close()
return [0.9]
def timerEvent(self, evt):
"""Custom timerEvent code, called upon timer event receive"""
# get the value from the device
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_())