Important: Please read the Qt Code of Conduct -

Reading input frequency and drawing using pyqtgraph

  • I have to read a square waveform of frequency 100Hz to 20kHz on GPIO of raspberry. I have checked using piscope the waveform at the required GPIO. Now, I need to draw a GUI window with some information regarding this square waveform. After reading online at various sites, I have finalized pyqtgraph and now I am trying to learn through some examples. In following code, I append a random value at some fixed time to the curve.

    from PyQt5 import QtWidgets, QtCore
    from pyqtgraph.Qt import QtGui
    import pyqtgraph as pg
    import sys
    import os
    from random import randint
    class MainWindow(QtWidgets.QMainWindow):
        def __init__(self,*args,**kwargs):
            self.graphWidget = pg.PlotWidget()
            pen = pg.mkPen(color='r', width=3)
            styles = {'color': '#f00', 'font-size': '20px'}
            self.graphWidget.setLabel('left', 'Temperature (°C)', **styles)
            self.graphWidget.setLabel('bottom', 'Hour (H)', **styles)
            self.graphWidget.setTitle("Frequency Plotting", color="b", size="30pt")
            # Add grid
            self.graphWidget.showGrid(x=True, y=True)
            self.x = list(range(100))  # 100 time points
            self.y = [randint(0, 100) for _ in range(100)]  # 100 data points
            self.lineCurve = self.graphWidget.plot(self.x, self.y,pen=pen)
            self.timer = QtCore.QTimer()
        def update_plot_data(self):
            # self.x = self.x[1:]  # Remove the first y element.
            # self.x.append(self.x[-1] + 1)  # Add a new value 1 higher than the last.
            self.y = self.y[1:]  # Remove the first
            self.y.append(randint(0, 100))  # Add a new random value.
            self.lineCurve.setData(self.x, self.y)  # Update the data.
    def main():
        app = QtWidgets.QApplication(sys.argv)
        window = MainWindow()
    if __name__ == '__main__':

    Now, I am stuck at how to combine reading square waveform frequency from GPIO and appending this to the array for updating to the waveform. Also, instead of single centralwidget, I require to add some labels with vertical layout.

  • Lifetime Qt Champion

    @Maanas Not sure what the problem is exactly?
    Can you already read GPIO?

    "I require to add some labels with vertical layout" - what is the problem with that?

  • I have read GPIO frequency using pigpio library. Now how to update it in pyqtgraph? The given example appends random value. But how to draw square waveform of the read frequency and also update it regularly.

    After setting centralWidget, I am not able to change layout or add vertical layout. How to do that?

  • @Maanas said in Reading input frequency and drawing using pyqtgraph:

    After setting centralWidget, I am not able to change layout or add vertical layout. How to do that?

    Set your central widget to e.g. a plain QWidget and then add desired layouts + sub-widgets on that as per normal.

  • Lifetime Qt Champion

    @Maanas said in Reading input frequency and drawing using pyqtgraph:

    and also update it regularly

    I don't know how exactly you're reading GPIO. You could use a QTimer and add new values you cololected from GPIO to your graph on each timeout.

  • I am only reading frequency value using pigpio module. I have to draw square waveform with updated frequency. The waveform display need not to be real time i.e. I can sample the frequency and later on change the waveform accordingly after some time. My query is how to draw this square waveform.

  • @JonB Can you please give an example

  • @Maanas
    Of adding layouts and then widgets to a QWidget? No, you must know perfectly well how to do that. There's nothing to say.

Log in to reply