2012-04-28 19:48:49 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-02-25 04:09:03 +00:00
|
|
|
"""
|
|
|
|
Demonstrates use of GLScatterPlotItem with rapidly-updating plots.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2012-04-28 19:48:49 +00:00
|
|
|
## Add path to library (just for examples; you do not need this)
|
2012-12-26 22:51:52 +00:00
|
|
|
import initExample
|
2012-04-28 19:48:49 +00:00
|
|
|
|
|
|
|
from pyqtgraph.Qt import QtCore, QtGui
|
|
|
|
import pyqtgraph.opengl as gl
|
2012-10-26 04:09:47 +00:00
|
|
|
import numpy as np
|
2012-04-28 19:48:49 +00:00
|
|
|
|
|
|
|
app = QtGui.QApplication([])
|
|
|
|
w = gl.GLViewWidget()
|
|
|
|
w.opts['distance'] = 20
|
|
|
|
w.show()
|
2013-02-25 04:09:03 +00:00
|
|
|
w.setWindowTitle('pyqtgraph example: GLScatterPlotItem')
|
2012-04-28 19:48:49 +00:00
|
|
|
|
|
|
|
g = gl.GLGridItem()
|
|
|
|
w.addItem(g)
|
|
|
|
|
2012-10-27 01:47:45 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
## First example is a set of points with pxMode=False
|
|
|
|
## These demonstrate the ability to have points with real size down to a very small scale
|
|
|
|
##
|
|
|
|
pos = np.empty((53, 3))
|
|
|
|
size = np.empty((53))
|
|
|
|
color = np.empty((53, 4))
|
|
|
|
pos[0] = (1,0,0); size[0] = 0.5; color[0] = (1.0, 0.0, 0.0, 0.5)
|
|
|
|
pos[1] = (0,1,0); size[1] = 0.2; color[1] = (0.0, 0.0, 1.0, 0.5)
|
|
|
|
pos[2] = (0,0,1); size[2] = 2./3.; color[2] = (0.0, 1.0, 0.0, 0.5)
|
|
|
|
|
|
|
|
z = 0.5
|
|
|
|
d = 6.0
|
|
|
|
for i in range(3,53):
|
|
|
|
pos[i] = (0,0,z)
|
|
|
|
size[i] = 2./d
|
|
|
|
color[i] = (0.0, 1.0, 0.0, 0.5)
|
|
|
|
z *= 0.5
|
|
|
|
d *= 2.0
|
2012-10-26 04:09:47 +00:00
|
|
|
|
2012-10-27 01:47:45 +00:00
|
|
|
sp1 = gl.GLScatterPlotItem(pos=pos, size=size, color=color, pxMode=False)
|
|
|
|
sp1.translate(5,5,0)
|
|
|
|
w.addItem(sp1)
|
|
|
|
|
2012-10-26 04:09:47 +00:00
|
|
|
|
2012-10-27 01:47:45 +00:00
|
|
|
##
|
|
|
|
## Second example shows a volume of points with rapidly updating color
|
|
|
|
## and pxMode=True
|
|
|
|
##
|
2012-10-26 04:09:47 +00:00
|
|
|
|
2012-10-27 01:47:45 +00:00
|
|
|
pos = np.random.random(size=(100000,3))
|
|
|
|
pos *= [10,-10,10]
|
|
|
|
pos[0] = (0,0,0)
|
2012-10-26 04:09:47 +00:00
|
|
|
color = np.ones((pos.shape[0], 4))
|
2012-10-27 01:47:45 +00:00
|
|
|
d2 = (pos**2).sum(axis=1)**0.5
|
|
|
|
size = np.random.random(size=pos.shape[0])*10
|
|
|
|
sp2 = gl.GLScatterPlotItem(pos=pos, color=(1,1,1,1), size=size)
|
2012-10-26 04:09:47 +00:00
|
|
|
phase = 0.
|
|
|
|
|
2012-10-27 01:47:45 +00:00
|
|
|
w.addItem(sp2)
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
## Third example shows a grid of points with rapidly updating position
|
|
|
|
## and pxMode = False
|
|
|
|
##
|
|
|
|
|
|
|
|
pos3 = np.zeros((100,100,3))
|
|
|
|
pos3[:,:,:2] = np.mgrid[:100, :100].transpose(1,2,0) * [-0.1,0.1]
|
|
|
|
pos3 = pos3.reshape(10000,3)
|
|
|
|
d3 = (pos3**2).sum(axis=1)**0.5
|
|
|
|
|
|
|
|
sp3 = gl.GLScatterPlotItem(pos=pos3, color=(1,1,1,.3), size=0.1, pxMode=False)
|
|
|
|
|
|
|
|
w.addItem(sp3)
|
|
|
|
|
|
|
|
|
2012-10-26 04:09:47 +00:00
|
|
|
def update():
|
2012-10-27 01:47:45 +00:00
|
|
|
## update volume colors
|
|
|
|
global phase, sp2, d2
|
|
|
|
s = -np.cos(d2*2+phase)
|
|
|
|
color = np.empty((len(d2),4), dtype=np.float32)
|
|
|
|
color[:,3] = np.clip(s * 0.1, 0, 1)
|
2012-10-26 04:09:47 +00:00
|
|
|
color[:,0] = np.clip(s * 3.0, 0, 1)
|
|
|
|
color[:,1] = np.clip(s * 1.0, 0, 1)
|
|
|
|
color[:,2] = np.clip(s ** 3, 0, 1)
|
2012-10-27 01:47:45 +00:00
|
|
|
sp2.setData(color=color)
|
2012-10-26 04:09:47 +00:00
|
|
|
phase -= 0.1
|
|
|
|
|
2012-10-27 01:47:45 +00:00
|
|
|
## update surface positions and colors
|
|
|
|
global sp3, d3, pos3
|
|
|
|
z = -np.cos(d3*2+phase)
|
|
|
|
pos3[:,2] = z
|
|
|
|
color = np.empty((len(d3),4), dtype=np.float32)
|
|
|
|
color[:,3] = 0.3
|
|
|
|
color[:,0] = np.clip(z * 3.0, 0, 1)
|
|
|
|
color[:,1] = np.clip(z * 1.0, 0, 1)
|
|
|
|
color[:,2] = np.clip(z ** 3, 0, 1)
|
|
|
|
sp3.setData(pos=pos3, color=color)
|
|
|
|
|
2012-10-26 04:09:47 +00:00
|
|
|
t = QtCore.QTimer()
|
|
|
|
t.timeout.connect(update)
|
|
|
|
t.start(50)
|
|
|
|
|
2012-04-28 19:48:49 +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_()
|