pyqtgraph/examples/ViewBox.py

102 lines
2.7 KiB
Python
Raw Normal View History

2010-03-22 05:48:52 +00:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
## Add path to library (just for examples; you do not need this)
import initExample
2010-03-22 05:48:52 +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
import pyqtgraph as pg
2010-03-22 05:48:52 +00:00
app = QtGui.QApplication([])
mw = QtGui.QMainWindow()
#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)
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)
#vl.addWidget(gv)
2010-03-22 05:48:52 +00:00
vb = pg.ViewBox()
#grid = pg.GridItem()
#vb.addItem(grid)
p1 = pg.PlotDataItem()
2010-03-22 05:48:52 +00:00
vb.addItem(p1)
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)
l.addItem(vb, 0, 1)
2010-03-22 05:48:52 +00:00
gv.centralWidget.setLayout(l)
xScale = pg.AxisItem(orientation='bottom', linkView=vb)
l.addItem(xScale, 1, 1)
yScale = pg.AxisItem(orientation='left', linkView=vb)
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")
yScale.setLabel('Y Axis', units='V')
2010-03-22 05:48:52 +00:00
def rand(n):
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
return data, np.arange(n, n+len(data)) / float(n)
2010-03-22 05:48:52 +00:00
def updateData():
yd, xd = rand(10000)
p1.setData(y=yd, x=xd)
2010-03-22 05:48:52 +00:00
yd, xd = rand(10000)
updateData()
vb.autoRange()
t = QtCore.QTimer()
t.timeout.connect(updateData)
2010-03-22 05:48:52 +00:00
t.start(50)
## Start Qt event loop unless running in interactive mode.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()