reflow/qtplot.py

173 lines
5.0 KiB
Python
Raw Normal View History

2012-11-10 14:27:43 +00:00
import sys, os, random
from PyQt4 import QtGui, QtCore
from numpy import arange, sin, pi, array, linspace
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.path import Path
import matplotlib.patches as patches
from scipy.interpolate import *
progname = os.path.basename(sys.argv[0])
progversion = "0.1"
class MyMplCanvas(FigureCanvas):
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
# We want the axes cleared every time plot() is called
self.axes.hold(False)
self.compute_initial_figure()
#
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,
QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
def compute_initial_figure(self):
pass
class MyDynamicMplCanvas(MyMplCanvas):
"""A canvas that updates itself every second with a new plot."""
def __init__(self, *args, **kwargs):
MyMplCanvas.__init__(self, *args, **kwargs)
timer = QtCore.QTimer(self)
QtCore.QObject.connect(timer, QtCore.SIGNAL("timeout()"), self.update_figure)
timer.start(1000)
def compute_initial_figure(self):
x1_min = 150-20.
y1_min = 150.
x2_min = x1_min + 100.
y2_min = 200.
x3_min = x2_min + 20.
y3_min = 220.
x4_min = x3_min + 249. - y3_min
y4_min = 249.
x5_min = x4_min + (y4_min - y3_min) / 3 * 2
y5_min = y3_min
x6_min = x5_min + y5_min-20.
y6_min = 20.
p1x = array([0., x1_min, x2_min, x3_min, x4_min, x5_min, x6_min])
p1y = array([20., y1_min, y2_min, y3_min, y4_min, y5_min, y6_min])
interp = pchip(p1x, p1y)
xx = linspace(0., x6_min, x6_min)
ynew = interp(xx)
print "len xx", len(xx)
print "len p1x", len(p1x)
print "xy", zip(p1x, p1y)
print "ynew", ynew
dtp_min = 99999.
dtp_max = 0.
dtpi = -1
dtn_min = -99999.
dtn_max = 0.
dtni = -1
for i in xrange(1, len(ynew)):
tmp = ynew[i] - ynew[i-1]
if tmp > 0:
if tmp < dtp_min:
dtp_min = tmp
dtpi = i
elif tmp > dtp_max:
dtp_max = tmp
dtpi = i
elif tmp < 0:
if tmp > dtn_min:
dtn_min = tmp
dtni = i
elif tmp < dtn_max:
dtn_max = tmp
dtni = i
print "max negative", dtn_min, dtn_max, dtni
print "max positive", dtp_min, dtp_max, dtpi
self.axes.plot(p1x, p1y, "bo", xx, ynew, "r-")
#self.axes.plot(p1x, p1y, 'r-o')
def update_figure(self):
# Build a list of 4 random integers between 0 and 10 (both inclusive)
#l = [ random.randint(0, 10) for i in xrange(4) ]
#self.axes.plot([0, 1, 2, 3], l, 'r')
#self.draw()
pass
class ApplicationWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setWindowTitle("application main window")
self.file_menu = QtGui.QMenu('&File', self)
self.file_menu.addAction('&Quit', self.fileQuit,
QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
self.menuBar().addMenu(self.file_menu)
self.help_menu = QtGui.QMenu('&Help', self)
self.menuBar().addSeparator()
self.menuBar().addMenu(self.help_menu)
self.help_menu.addAction('&About', self.about)
self.main_widget = QtGui.QWidget(self)
l = QtGui.QVBoxLayout(self.main_widget)
#sc = MyStaticMplCanvas(self.main_widget, width=5, height=4, dpi=100)
dc = MyDynamicMplCanvas(self.main_widget, width=5, height=4, dpi=100)
#l.addWidget(sc)
l.addWidget(dc)
self.main_widget.setFocus()
self.setCentralWidget(self.main_widget)
self.statusBar().showMessage("All hail matplotlib!", 2000)
def fileQuit(self):
self.close()
def closeEvent(self, ce):
self.fileQuit()
def about(self):
QtGui.QMessageBox.about(self, "About %s" % progname,
u"""%(prog)s version %(version)s
Copyright \N{COPYRIGHT SIGN} 2005 Florent Rougon, 2006 Darren Dale
This program is a simple example of a Qt4 application embedding matplotlib
canvases.
It may be used and modified with no restriction; raw copies as well as
modified versions may be distributed without limitation."""
% {"prog": progname, "version": progversion})
qApp = QtGui.QApplication(sys.argv)
aw = ApplicationWindow()
aw.setWindowTitle("%s" % progname)
aw.show()
sys.exit(qApp.exec_())