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