2010-03-22 05:48:52 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
2013-02-25 04:09:03 +00:00
|
|
|
"""
|
|
|
|
ViewBox is the general-purpose graphical container that allows the user to
|
|
|
|
zoom / pan to inspect any area of a 2D coordinate system.
|
|
|
|
|
2021-08-04 20:47:08 +00:00
|
|
|
This unimaginative example demonstrates the construction of a ViewBox-based
|
2013-02-25 04:09:03 +00:00
|
|
|
plot area with axes, very similar to the way PlotItem is built.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2010-03-22 06:21:56 +00:00
|
|
|
## Add path to library (just for examples; you do not need this)
|
2012-12-26 22:51:52 +00:00
|
|
|
import initExample
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-01-27 18:59:07 +00:00
|
|
|
app = pg.mkQApp("ViewBox Example")
|
2010-03-22 05:48:52 +00:00
|
|
|
mw = QtGui.QMainWindow()
|
2013-02-25 04:09:03 +00:00
|
|
|
mw.setWindowTitle('pyqtgraph example: ViewBox')
|
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)
|
2010-03-22 05:48:52 +00:00
|
|
|
l = QtGui.QGraphicsGridLayout()
|
|
|
|
l.setHorizontalSpacing(0)
|
|
|
|
l.setVerticalSpacing(0)
|
|
|
|
|
2011-04-05 14:35:50 +00:00
|
|
|
vb = pg.ViewBox()
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
p1 = pg.PlotDataItem()
|
2010-03-22 05:48:52 +00:00
|
|
|
vb.addItem(p1)
|
2010-07-24 18:29:09 +00:00
|
|
|
|
2013-02-25 18:03:21 +00:00
|
|
|
## Just something to play with inside the ViewBox
|
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()
|
2015-02-28 16:26:45 +00:00
|
|
|
self.setPen(pg.mkPen(255, 255, 255))
|
2010-07-24 18:29:09 +00:00
|
|
|
ev.ignore()
|
|
|
|
def hoverLeaveEvent(self, ev):
|
|
|
|
self.setPen(self.savedPen)
|
|
|
|
ev.ignore()
|
|
|
|
def mousePressEvent(self, ev):
|
2021-06-06 01:17:43 +00:00
|
|
|
if ev.button() == QtCore.Qt.MouseButton.LeftButton:
|
2010-07-24 18:29:09 +00:00
|
|
|
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 = movableRect(QtCore.QRectF(0, 0, 1, 1))
|
2015-02-28 16:26:45 +00:00
|
|
|
rect.setPen(pg.mkPen(100, 200, 100))
|
2010-03-22 05:48:52 +00:00
|
|
|
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)
|
|
|
|
|
2012-12-05 05:25:45 +00:00
|
|
|
if __name__ == '__main__':
|
2021-05-13 21:28:22 +00:00
|
|
|
pg.exec()
|