Search code examples
pythonpyqtgraph

Make each Y value to correspond to an X value in pyqtgraph


I have an algorithm which takes some data and plots them in real time. I have a pyqt5 window and I use pyqtgraph to plot on the window. The code snippet I use is below;

import pyqtgraph as pg
from PyQt5.QtWidgets import QMainWindow
from Ui_GraphicsLayout import Ui_GraphicsLayout

class TimeDomainPlotWindow(QMainWindow):
    closing = pyqtSignal()
    def __init__(self, title = "Time Domain Plot", name = "Channel"):
        super().__init__()

        pg.setConfigOption('background', 'w')   

        self.__ui = Ui_GraphicsLayout()
        self.__ui.setupUi(self)

        self.setWindowTitle("Real Time Data - {:s}".format(title))
        self.__plot = self.__ui.widget.addPlot(title = name, row = 0, col = 0)
        self.__pditem = self.__plot.plot(pen = 'k')

    def plot(self, data):
        self.__pditem.setData(data)

The plot I get looks like this: enter image description here

So I send an array of 1000 values and it plots them. Because I have 1000 values in my array it numbers the X axis from 0 to 1000. I want to change the range of values in X axis and make each value in my data array correspond to an X value in the graph. So just like in excel where you have two columns of data and one of them is your X axis and the other is your Y axis. I want to, lets say have an X axis that has values from 1 to 100 but just have 10 Y values and make each of those 10 values be a point at a specific X axis value. Is it possible to do this?


Solution

  • The setData function has an optional x argument which does what you're describing, so your plot function would become:

        def plot(self, data, x_list):
            self.__pditem.setData(y=data, x=x_list)
    

    where x_list is a list with the same length as data.

    To change the x-axis to 1 to 100 you'd use the setXRange function, see this documentation