2013/07/03

PySide, PySerial, Matplotlib

아따 어렵네...


import sys
from PySide.QtCore import *
from PySide.QtGui import *
import serial

import matplotlib
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
import pylab

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

import threading, random

class TB_Form02(QDialog):
def update_data(self):
tmp = random.random()
self.data1.append(tmp)
tmp = random.random()
self.data2.append(tmp)
del self.data1[0]
del self.data2[0]

self.ax1.cla()
self.ax1.plot(range(50),self.data1)
self.ax2.cla()
self.ax2.plot(range(50),self.data2)
self.canvas.draw()

t = threading.Timer(1.0, self.update_data)
t.start()

def __init__(self, parent = None):
super(TB_Form02, self).__init__(parent)

self.data1 = [0]*50
self.data2 = [0]*50
# generate the plot
self.fig = Figure(figsize=(50,50), dpi=72, facecolor=(1,1,1), edgecolor=(0,0,0))
self.ax1 = self.fig.add_subplot(211)
self.ax2 = self.fig.add_subplot(212)
self.ax1.plot(range(50),self.data1)
self.ax2.plot(range(50),self.data2)

# generate the canvas to display the plot
self.canvas = FigureCanvas(self.fig)
self.canvas.setFixedSize(250,200)
self.update_data()

FRAME = QHBoxLayout()
FRAME.addWidget(self.canvas)
self.setLayout(FRAME)


if __name__ == '__main__':
app = QApplication(sys.argv)
form = TB_Form02()
form.show()

sys.exit(app.exec_())