pyqtplot15.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python
  2. # File: pyqtplot15.py
  3. # Name: D.Saravanan
  4. # Date: 21/07/2023
  5. """ Script to create plot using the PlotWidget in PyQtGraph """
  6. import sys
  7. import numpy as np
  8. import pyqtgraph as pg
  9. from PyQt6 import QtCore, QtWidgets
  10. pg.setConfigOptions(antialias=True)
  11. class MainWindow(QtWidgets.QMainWindow):
  12. """Subclass of QMainWindow to customize application's main window."""
  13. def __init__(self, xval, wav1, wav2):
  14. super().__init__()
  15. self.xval = xval
  16. self.wav1 = wav1
  17. self.wav2 = wav2
  18. self.nval = 0
  19. # set the size parameters (width, height) pixels
  20. self.setFixedSize(QtCore.QSize(640, 480))
  21. # set the central widget of the window
  22. self.graphWidget = pg.PlotWidget()
  23. self.setCentralWidget(self.graphWidget)
  24. # set the background color using hex notation #121317 as string
  25. self.graphWidget.setBackground("#121317")
  26. # set the main plot title, text color, text size, text weight, text style
  27. self.graphWidget.setTitle(
  28. "Sine and Cosine", color="#dcdcdc", size="10pt", bold=True, italic=False
  29. )
  30. # set the axis labels (position and text), style parameters
  31. styles = {"color": "#dcdcdc", "font-size": "10pt"}
  32. self.graphWidget.setLabel("left", "f(x)", **styles)
  33. self.graphWidget.setLabel("bottom", "x", **styles)
  34. # set the legend which represents given line
  35. self.graphWidget.addLegend(offset=(-10, 10))
  36. # set the background grid for both the x and y axis
  37. self.graphWidget.showGrid(x=True, y=True, alpha=0.5)
  38. # graphPlot method call
  39. self.graphPlot(self.xval, self.wav1, self.wav2)
  40. def graphPlot(self, xval, wav1, wav2):
  41. """Method accepts x and y parameters to plot."""
  42. # set the axis limits within the specified ranges and padding
  43. self.graphWidget.setXRange(xval[0], xval[-1], padding=0)
  44. self.graphWidget.setYRange(min(wav1), max(wav1), padding=0.1)
  45. # plot data: x, y values with lines drawn using Qt's QPen types
  46. self.data_line1 = self.graphWidget.plot(
  47. xval[0:1], wav1[0:1], name="sin(x)", pen=pg.mkPen(color="#fa8775")
  48. )
  49. self.data_line2 = self.graphWidget.plot(
  50. xval[0:1], wav2[0:1], name="cos(x)", pen=pg.mkPen(color="#3574e2")
  51. )
  52. self.timer = QtCore.QTimer()
  53. self.timer.setInterval(50)
  54. self.timer.timeout.connect(self.update_data_line)
  55. self.timer.start()
  56. def update_data_line(self):
  57. """Method uses QTimer to update the data every 50ms."""
  58. self.nval = self.nval + 1
  59. self.data_line1.setData(self.xval[0 : self.nval], self.wav1[0 : self.nval])
  60. self.data_line2.setData(self.xval[0 : self.nval], self.wav2[0 : self.nval])
  61. def main():
  62. """Need one (and only one) QApplication instance per application.
  63. Pass in sys.argv to allow command line arguments for the application.
  64. If no command line arguments than QApplication([]) is required."""
  65. app = QtWidgets.QApplication(sys.argv)
  66. xval = np.linspace(-2 * np.pi, 2 * np.pi, 1000, retstep=False)
  67. # an instance of the class MainWindow
  68. window = MainWindow(xval, np.sin(xval), np.cos(xval))
  69. window.show() # windows are hidden by default
  70. sys.exit(app.exec()) # start the event loop
  71. if __name__ == "__main__":
  72. main()