2010-03-22 05:48:52 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
2010-03-22 06:21:56 +00:00
|
|
|
## Add path to library (just for examples; you do not need this)
|
|
|
|
import sys, os
|
2011-04-05 16:53:05 +00:00
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
|
2010-03-22 05:48:52 +00:00
|
|
|
|
2011-04-05 14:35:50 +00:00
|
|
|
## This example uses a ViewBox to create a PlotWidget-like interface
|
|
|
|
|
|
|
|
#from scipy import random
|
|
|
|
import numpy as np
|
2012-03-02 03:53:52 +00:00
|
|
|
from pyqtgraph.Qt import QtGui, QtCore
|
2011-04-05 14:35:50 +00:00
|
|
|
import pyqtgraph as pg
|
2010-03-22 05:48:52 +00:00
|
|
|
|
|
|
|
app = QtGui.QApplication([])
|
|
|
|
mw = QtGui.QMainWindow()
|
2012-03-02 02:55:32 +00:00
|
|
|
#cw = QtGui.QWidget()
|
|
|
|
#vl = QtGui.QVBoxLayout()
|
|
|
|
#cw.setLayout(vl)
|
|
|
|
#mw.setCentralWidget(cw)
|
2010-03-22 05:48:52 +00:00
|
|
|
mw.show()
|
|
|
|
mw.resize(800, 600)
|
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
gv = pg.GraphicsView()
|
|
|
|
mw.setCentralWidget(gv)
|
|
|
|
#gv.enableMouse(False) ## Mouse interaction will be handled by the ViewBox
|
2010-03-22 05:48:52 +00:00
|
|
|
l = QtGui.QGraphicsGridLayout()
|
|
|
|
l.setHorizontalSpacing(0)
|
|
|
|
l.setVerticalSpacing(0)
|
2012-03-02 02:55:32 +00:00
|
|
|
#vl.addWidget(gv)
|
2010-03-22 05:48:52 +00:00
|
|
|
|
|
|
|
|
2011-04-05 14:35:50 +00:00
|
|
|
vb = pg.ViewBox()
|
2012-03-02 02:55:32 +00:00
|
|
|
#grid = pg.GridItem()
|
|
|
|
#vb.addItem(grid)
|
|
|
|
|
|
|
|
p1 = pg.PlotDataItem()
|
2010-03-22 05:48:52 +00:00
|
|
|
vb.addItem(p1)
|
2010-07-24 18:29:09 +00:00
|
|
|
|
|
|
|
class movableRect(QtGui.QGraphicsRectItem):
|
|
|
|
def __init__(self, *args):
|
|
|
|
QtGui.QGraphicsRectItem.__init__(self, *args)
|
|
|
|
self.setAcceptHoverEvents(True)
|
|
|
|
def hoverEnterEvent(self, ev):
|
|
|
|
self.savedPen = self.pen()
|
|
|
|
self.setPen(QtGui.QPen(QtGui.QColor(255, 255, 255)))
|
|
|
|
ev.ignore()
|
|
|
|
def hoverLeaveEvent(self, ev):
|
|
|
|
self.setPen(self.savedPen)
|
|
|
|
ev.ignore()
|
|
|
|
def mousePressEvent(self, ev):
|
|
|
|
if ev.button() == QtCore.Qt.LeftButton:
|
|
|
|
ev.accept()
|
|
|
|
self.pressDelta = self.mapToParent(ev.pos()) - self.pos()
|
|
|
|
else:
|
|
|
|
ev.ignore()
|
|
|
|
def mouseMoveEvent(self, ev):
|
|
|
|
self.setPos(self.mapToParent(ev.pos()) - self.pressDelta)
|
|
|
|
|
|
|
|
|
|
|
|
#rect = QtGui.QGraphicsRectItem(QtCore.QRectF(0, 0, 1, 1))
|
|
|
|
rect = movableRect(QtCore.QRectF(0, 0, 1, 1))
|
2010-03-22 05:48:52 +00:00
|
|
|
rect.setPen(QtGui.QPen(QtGui.QColor(100, 200, 100)))
|
|
|
|
vb.addItem(rect)
|
|
|
|
|
2010-03-22 06:21:56 +00:00
|
|
|
l.addItem(vb, 0, 1)
|
2010-03-22 05:48:52 +00:00
|
|
|
gv.centralWidget.setLayout(l)
|
|
|
|
|
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
xScale = pg.AxisItem(orientation='bottom', linkView=vb)
|
2010-03-22 06:21:56 +00:00
|
|
|
l.addItem(xScale, 1, 1)
|
2012-03-02 02:55:32 +00:00
|
|
|
yScale = pg.AxisItem(orientation='left', linkView=vb)
|
2010-03-22 06:21:56 +00:00
|
|
|
l.addItem(yScale, 0, 0)
|
2010-03-22 05:48:52 +00:00
|
|
|
|
2012-05-11 22:05:41 +00:00
|
|
|
xScale.setLabel(text="<span style='color: #ff0000; font-weight: bold'>X</span> <i>Axis</i>", units="s")
|
2010-03-22 06:21:56 +00:00
|
|
|
yScale.setLabel('Y Axis', units='V')
|
2010-03-22 05:48:52 +00:00
|
|
|
|
|
|
|
def rand(n):
|
2011-04-05 14:35:50 +00:00
|
|
|
data = np.random.random(n)
|
2010-03-22 05:48:52 +00:00
|
|
|
data[int(n*0.1):int(n*0.13)] += .5
|
|
|
|
data[int(n*0.18)] += 2
|
|
|
|
data[int(n*0.1):int(n*0.13)] *= 5
|
|
|
|
data[int(n*0.18)] *= 20
|
2011-04-05 14:35:50 +00:00
|
|
|
return data, np.arange(n, n+len(data)) / float(n)
|
2010-03-22 05:48:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def updateData():
|
|
|
|
yd, xd = rand(10000)
|
2012-03-02 02:55:32 +00:00
|
|
|
p1.setData(y=yd, x=xd)
|
2010-03-22 05:48:52 +00:00
|
|
|
|
|
|
|
yd, xd = rand(10000)
|
|
|
|
updateData()
|
|
|
|
vb.autoRange()
|
|
|
|
|
|
|
|
t = QtCore.QTimer()
|
2011-04-05 14:35:50 +00:00
|
|
|
t.timeout.connect(updateData)
|
2010-03-22 05:48:52 +00:00
|
|
|
t.start(50)
|
|
|
|
|
2011-04-05 14:35:50 +00:00
|
|
|
## Start Qt event loop unless running in interactive mode.
|
2012-12-05 05:25:45 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
|
|
|
|
QtGui.QApplication.instance().exec_()
|