|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | + Animated 3D sinc function |
| 4 | +
|
| 5 | + requires: |
| 6 | + 1. pyqtgraph |
| 7 | + - download from here http://www.pyqtgraph.org/ |
| 8 | + 2. pyopenGL |
| 9 | + - if you have Anaconda, run the following command |
| 10 | + >>> conda install -c anaconda pyopengl |
| 11 | +""" |
| 12 | + |
| 13 | +from pyqtgraph.Qt import QtCore, QtGui |
| 14 | +import pyqtgraph.opengl as gl |
| 15 | +import pyqtgraph as pg |
| 16 | +import numpy as np |
| 17 | +import sys |
| 18 | +import time |
| 19 | + |
| 20 | + |
| 21 | +class Visualizer(object): |
| 22 | + def __init__(self): |
| 23 | + self.traces = dict() |
| 24 | + self.app = QtGui.QApplication(sys.argv) |
| 25 | + self.w = gl.GLViewWidget() |
| 26 | + self.w.opts['distance'] = 40 |
| 27 | + self.w.setWindowTitle('pyqtgraph example: GLLinePlotItem') |
| 28 | + self.w.setGeometry(0, 110, 1920, 1080) |
| 29 | + self.w.show() |
| 30 | + |
| 31 | + self.phase = 0 |
| 32 | + self.lines = 50 |
| 33 | + self.points = 1000 |
| 34 | + self.y = np.linspace(-10, 10, self.lines) |
| 35 | + self.x = np.linspace(-10, 10, self.points) |
| 36 | + |
| 37 | + for i, line in enumerate(self.y): |
| 38 | + y = np.array([line] * self.points) |
| 39 | + d = np.sqrt(self.x ** 2 + y ** 2) |
| 40 | + sine = 10 * np.sin(d + self.phase) |
| 41 | + pts = np.vstack([self.x, y, sine]).transpose() |
| 42 | + self.traces[i] = gl.GLLinePlotItem( |
| 43 | + pos=pts, |
| 44 | + color=pg.glColor((i, self.lines * 1.3)), |
| 45 | + width=(i + 1) / 10, |
| 46 | + antialias=True |
| 47 | + ) |
| 48 | + self.w.addItem(self.traces[i]) |
| 49 | + |
| 50 | + def start(self): |
| 51 | + if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): |
| 52 | + QtGui.QApplication.instance().exec_() |
| 53 | + |
| 54 | + def set_plotdata(self, name, points, color, width): |
| 55 | + self.traces[name].setData(pos=points, color=color, width=width) |
| 56 | + |
| 57 | + def update(self): |
| 58 | + stime = time.time() |
| 59 | + for i, line in enumerate(self.y): |
| 60 | + y = np.array([line] * self.points) |
| 61 | + |
| 62 | + amp = 10 / (i + 1) |
| 63 | + phase = self.phase * (i + 1) - 10 |
| 64 | + freq = self.x * (i + 1) / 10 |
| 65 | + |
| 66 | + sine = amp * np.sin(freq - phase) |
| 67 | + pts = np.vstack([self.x, y, sine]).transpose() |
| 68 | + |
| 69 | + self.set_plotdata( |
| 70 | + name=i, points=pts, |
| 71 | + color=pg.glColor((i, self.lines * 1.3)), |
| 72 | + width=3 |
| 73 | + ) |
| 74 | + self.phase -= .0002 |
| 75 | + |
| 76 | + print('{:.0f} FPS'.format(1 / (time.time() - stime))) |
| 77 | + |
| 78 | + def animation(self): |
| 79 | + timer = QtCore.QTimer() |
| 80 | + timer.timeout.connect(self.update) |
| 81 | + timer.start(10) |
| 82 | + self.start() |
| 83 | + |
| 84 | + |
| 85 | +# Start event loop. |
| 86 | +if __name__ == '__main__': |
| 87 | + v = Visualizer() |
| 88 | + v.animation() |
0 commit comments