/pyqtgraph-0.9.10/examples/ScatterPlot.py

https://gitlab.com/pkormos/phase_python
Python | 102 lines | 57 code | 25 blank | 20 comment | 9 complexity | e8bb53de96fe489b3c763a1624775a73 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. """
  3. Example demonstrating a variety of scatter plot features.
  4. """
  5. ## Add path to library (just for examples; you do not need this)
  6. import initExample
  7. from pyqtgraph.Qt import QtGui, QtCore
  8. import pyqtgraph as pg
  9. import numpy as np
  10. app = QtGui.QApplication([])
  11. mw = QtGui.QMainWindow()
  12. mw.resize(800,800)
  13. view = pg.GraphicsLayoutWidget() ## GraphicsView with GraphicsLayout inserted by default
  14. mw.setCentralWidget(view)
  15. mw.show()
  16. mw.setWindowTitle('pyqtgraph example: ScatterPlot')
  17. ## create four areas to add plots
  18. w1 = view.addPlot()
  19. w2 = view.addViewBox()
  20. w2.setAspectLocked(True)
  21. view.nextRow()
  22. w3 = view.addPlot()
  23. w4 = view.addPlot()
  24. print("Generating data, this takes a few seconds...")
  25. ## There are a few different ways we can draw scatter plots; each is optimized for different types of data:
  26. ## 1) All spots identical and transform-invariant (top-left plot).
  27. ## In this case we can get a huge performance boost by pre-rendering the spot
  28. ## image and just drawing that image repeatedly.
  29. n = 300
  30. s1 = pg.ScatterPlotItem(size=10, pen=pg.mkPen(None), brush=pg.mkBrush(255, 255, 255, 120))
  31. pos = np.random.normal(size=(2,n), scale=1e-5)
  32. spots = [{'pos': pos[:,i], 'data': 1} for i in range(n)] + [{'pos': [0,0], 'data': 1}]
  33. s1.addPoints(spots)
  34. w1.addItem(s1)
  35. ## Make all plots clickable
  36. lastClicked = []
  37. def clicked(plot, points):
  38. global lastClicked
  39. for p in lastClicked:
  40. p.resetPen()
  41. print("clicked points", points)
  42. for p in points:
  43. p.setPen('b', width=2)
  44. lastClicked = points
  45. s1.sigClicked.connect(clicked)
  46. ## 2) Spots are transform-invariant, but not identical (top-right plot).
  47. ## In this case, drawing is almsot as fast as 1), but there is more startup
  48. ## overhead and memory usage since each spot generates its own pre-rendered
  49. ## image.
  50. s2 = pg.ScatterPlotItem(size=10, pen=pg.mkPen('w'), pxMode=True)
  51. pos = np.random.normal(size=(2,n), scale=1e-5)
  52. spots = [{'pos': pos[:,i], 'data': 1, 'brush':pg.intColor(i, n), 'symbol': i%5, 'size': 5+i/10.} for i in range(n)]
  53. s2.addPoints(spots)
  54. w2.addItem(s2)
  55. s2.sigClicked.connect(clicked)
  56. ## 3) Spots are not transform-invariant, not identical (bottom-left).
  57. ## This is the slowest case, since all spots must be completely re-drawn
  58. ## every time because their apparent transformation may have changed.
  59. s3 = pg.ScatterPlotItem(pxMode=False) ## Set pxMode=False to allow spots to transform with the view
  60. spots3 = []
  61. for i in range(10):
  62. for j in range(10):
  63. spots3.append({'pos': (1e-6*i, 1e-6*j), 'size': 1e-6, 'pen': {'color': 'w', 'width': 2}, 'brush':pg.intColor(i*10+j, 100)})
  64. s3.addPoints(spots3)
  65. w3.addItem(s3)
  66. s3.sigClicked.connect(clicked)
  67. ## Test performance of large scatterplots
  68. s4 = pg.ScatterPlotItem(size=10, pen=pg.mkPen(None), brush=pg.mkBrush(255, 255, 255, 20))
  69. pos = np.random.normal(size=(2,10000), scale=1e-9)
  70. s4.addPoints(x=pos[0], y=pos[1])
  71. w4.addItem(s4)
  72. s4.sigClicked.connect(clicked)
  73. ## Start Qt event loop unless running in interactive mode.
  74. if __name__ == '__main__':
  75. import sys
  76. if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
  77. QtGui.QApplication.instance().exec_()