pyqtplot12.py 3.4 KB

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