pyqtplot06.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python
  2. # File: pyqtplot06.py
  3. # Name: D.Saravanan
  4. # Date: 12/07/2023
  5. """ Script to create plot using the PlotWidget in PyQtGraph """
  6. import sys
  7. import pyqtgraph as pg
  8. from PyQt6 import QtCore, QtWidgets
  9. class MainWindow(QtWidgets.QMainWindow):
  10. """Subclass of QMainWindow to customize application's main window."""
  11. def __init__(self, hour, temperature):
  12. super().__init__()
  13. # set the size parameters (width, height) pixels
  14. self.setFixedSize(QtCore.QSize(640, 480))
  15. # set the central widget of the window
  16. self.graphWidget = pg.PlotWidget()
  17. self.setCentralWidget(self.graphWidget)
  18. # set the background color using hex notation #121317 as string
  19. self.graphWidget.setBackground("#121317")
  20. # set the main plot title, text color, text size, text weight, text style
  21. self.graphWidget.setTitle(
  22. "Temperature Plot", color="#dcdcdc", size="10pt", bold=True, italic=False
  23. )
  24. # set the axis labels (position and text), style parameters
  25. styles = {"color": "#dcdcdc", "font-size": "10pt"}
  26. self.graphWidget.setLabel(
  27. "left", "Temperature", units="\N{DEGREE SIGN}C", **styles
  28. )
  29. self.graphWidget.setLabel("bottom", "Hour", units="H", **styles)
  30. # set the line color in 3-tuple of int values, line width in pixels, line style
  31. lvalue = pg.mkPen(
  32. color=(220, 220, 220), width=1, style=QtCore.Qt.PenStyle.SolidLine
  33. )
  34. # plot data: x, y values with lines drawn using Qt's QPen types & marker '+'
  35. self.graphWidget.plot(
  36. hour, temperature, pen=lvalue, symbol="+", symbolSize=8, symbolBrush=("r")
  37. )
  38. def main():
  39. """Need one (and only one) QApplication instance per application.
  40. Pass in sys.argv to allow command line arguments for the application.
  41. If no command line arguments than QApplication([]) is required."""
  42. app = QtWidgets.QApplication(sys.argv)
  43. hour = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  44. temperature = [30, 32, 34, 32, 33, 31, 29, 32, 35, 45]
  45. window = MainWindow(hour, temperature) # an instance of the class MainWindow
  46. window.show() # windows are hidden by default
  47. sys.exit(app.exec()) # start the event loop
  48. if __name__ == "__main__":
  49. main()