Guest User

Untitled

a guest
Jul 15th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Animated 3D sinc function
  4. """
  5.  
  6. from pyqtgraph.Qt import QtCore, QtGui
  7. import pyqtgraph.opengl as gl
  8. import pyqtgraph as pg
  9. import numpy as np
  10. import sys
  11.  
  12.  
  13. class Visualizer(object):
  14. def __init__(self):
  15. self.traces = dict()
  16. self.app = QtGui.QApplication(sys.argv)
  17. self.w = gl.GLViewWidget()
  18. self.w.opts['distance'] = 40
  19. self.w.setWindowTitle('pyqtgraph example: GLLinePlotItem')
  20. self.w.setGeometry(0, 110, 800, 600)
  21. self.w.show()
  22.  
  23. # create the background grids
  24. gx = gl.GLGridItem()
  25. gx.rotate(90, 0, 1, 0)
  26. gx.translate(-10, 0, 0)
  27. self.w.addItem(gx)
  28. gy = gl.GLGridItem()
  29. gy.rotate(90, 1, 0, 0)
  30. gy.translate(0, -10, 0)
  31. self.w.addItem(gy)
  32. gz = gl.GLGridItem()
  33. gz.translate(0, 0, -10)
  34. self.w.addItem(gz)
  35.  
  36. self.n = 50
  37. self.m = 1000
  38. self.y = np.linspace(-10, 10, self.n)
  39. self.x = np.linspace(-10, 10, self.m)
  40. self.phase = 0
  41.  
  42. for i in range(self.n):
  43. yi = np.array([self.y[i]] * self.m)
  44. d = np.sqrt(self.x ** 2 + yi ** 2)
  45. z = 10 * np.cos(d + self.phase) / (d + 1)
  46. pts = np.vstack([self.x, yi, z]).transpose()
  47. self.traces[i] = gl.GLLinePlotItem(pos=pts, color=pg.glColor(
  48. (i, self.n * 1.3)), width=(i + 1) / 10, antialias=True)
  49. self.w.addItem(self.traces[i])
  50.  
  51. def start(self):
  52. if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
  53. QtGui.QApplication.instance().exec()
  54.  
  55. def set_plotdata(self, name, points, color, width):
  56. self.traces[name].setData(pos=points, color=color, width=width)
  57.  
  58. def update(self):
  59. for i in range(self.n):
  60. yi = np.array([self.y[i]] * self.m)
  61. d = np.sqrt(self.x ** 2 + yi ** 2)
  62. z = 10 * np.cos(d + self.phase) / (d + 1)
  63. pts = np.vstack([self.x, yi, z]).transpose()
  64. self.set_plotdata(
  65. name=i, points=pts,
  66. color=pg.glColor((i, self.n * 1.3)),
  67. width=(i + 1) / 10
  68. )
  69. self.phase -= .003
  70.  
  71. def animation(self):
  72. timer = QtCore.QTimer()
  73. timer.timeout.connect(self.update)
  74. # timer.start(20)
  75. timer.start(0)
  76. self.start()
  77.  
  78.  
  79. # Start Qt event loop unless running in interactive mode.
  80. if __name__ == '__main__':
  81. v = Visualizer()
  82. v.animation()
Add Comment
Please, Sign In to add comment