pg.PlotWidget graph np.array into GUI

2

I tried differents solutions to trace a np.array into a pg.PlotWidget into a GUI. Doesn't work, when I clik on my button

import sys
import pandas as pd
import re
import numpy as np
import matplotlib.pyplot as plt
import pyqtgraph as pg
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog

class Ui_MainWindow(object):
    def __init__(self):
        self.df = pd.DataFrame()
        self.f_SamplingFreq = 0.0
        self.My_List_Name_Measure = []
        self.str_Name_Measure_Current = ""
        self.index_Measure_Current = 0
        self.List_Data_Raw_Signal = []

    def setupUi(self, MainWindow):
        self.push_Open_Data = QtWidgets.QPushButton(self.centralwidget)
        self.push_Open_Data.setGeometry(QtCore.QRect(10, 10, 80, 21))
        self.push_Open_Data.setStyleSheet("color: rgb(255, 255, 255);\n"
        "background-color: rgb(117, 117, 117);")
        self.push_Open_Data.setObjectName("push_Open_Data")
        self.push_Open_Data.clicked.connect(self.on_click_Open_Data)
        self.graphicsView_SignalSource = pg.PlotWidget(self.centralwidget)
        self.graphicsView_SignalSource.setGeometry(QtCore.QRect(30, 80, 421, 291))
        self.graphicsView_SignalSource.setStyleSheet("background-color: rgb(44, 44, 44);")
        self.graphicsView_SignalSource.setObjectName("graphicsView_SignalSource")


    def on_click_Open_Data(self):
        x = np.array([1,45,8,9])
        y = np.array([1, 4, 9, 16])
        curve = pg.PlotCurveItem(pen='r')
        curve.setData(x)
        self.graphicsView_SignalSource.addItem(curve.getData())
def main():
    app = QtWidgets.QApplication(sys.argv)
    ex = Ui_MainWindow()
    w = QtWidgets.QMainWindow()
    ex.setupUi(w)
    w.show()
    sys.exit(app.exec_())

## Start Qt event loop unless running in interactive mode.
if __name__ == '__main__':
    main()

Process finished with exit code -1073740791 (0xC0000409)

Thank you for your help

python
pyqt5
asked on Stack Overflow Oct 13, 2018 by Jérôme Pannetier • edited Oct 13, 2018 by eyllanesc

1 Answer

0

I suppose you have eliminated elements like the centralwidget and in your original code you have it. On the other hand the underlying problem is that when you add an item you must add an item, not the data associated with an item, so your code should be as follows:

class Ui_MainWindow(object):
    def __init__(self):
        self.df = pd.DataFrame()
        self.f_SamplingFreq = 0.0
        self.My_List_Name_Measure = []
        self.str_Name_Measure_Current = ""
        self.index_Measure_Current = 0
        self.List_Data_Raw_Signal = []

    def setupUi(self, MainWindow):
        self.centralwidget = QtWidgets.QWidget() # <---
        MainWindow.setCentralWidget(self.centralwidget) # <---
        self.push_Open_Data = QtWidgets.QPushButton(self.centralwidget)
        self.push_Open_Data.setGeometry(QtCore.QRect(10, 10, 80, 21))
        self.push_Open_Data.setStyleSheet("color: rgb(255, 255, 255);\n"
        "background-color: rgb(117, 117, 117);")
        self.push_Open_Data.setObjectName("push_Open_Data")
        self.push_Open_Data.clicked.connect(self.on_click_Open_Data)
        self.graphicsView_SignalSource = pg.PlotWidget(self.centralwidget)
        self.graphicsView_SignalSource.setGeometry(QtCore.QRect(30, 80, 421, 291))
        self.graphicsView_SignalSource.setStyleSheet("background-color: rgb(44, 44, 44);")
        self.graphicsView_SignalSource.setObjectName("graphicsView_SignalSource")

    def on_click_Open_Data(self):
        x = np.array([1,45,8,9])
        y = np.array([1, 4, 9, 16])
        curve = pg.PlotCurveItem(pen='r')
        curve.setData(x)
        self.graphicsView_SignalSource.addItem(curve) # <---

On the other hand I recommend you to use layouts so that your design can handle the sizes without problems as I show below:

import sys
from PyQt5 import QtCore, QtWidgets
import pyqtgraph as pg
import numpy as np


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)

        button = QtWidgets.QPushButton()
        button.setFixedSize(80, 21)
        button.setStyleSheet("color: rgb(255, 255, 255);\n"
        "background-color: rgb(117, 117, 117);")
        button.clicked.connect(self.on_click_Open_Data)

        self.graphicsView_SignalSource = pg.PlotWidget()

        vlay = QtWidgets.QVBoxLayout(central_widget)
        hlay = QtWidgets.QHBoxLayout()
        hlay.addWidget(button)
        hlay.addStretch()
        vlay.addLayout(hlay)
        vlay.addWidget(self.graphicsView_SignalSource)

    @QtCore.pyqtSlot()
    def on_click_Open_Data(self):
        x = np.array([1,45,8,9])
        y = np.array([1, 4, 9, 16])
        curve = pg.PlotCurveItem(pen='r')
        curve.setData(x)
        self.graphicsView_SignalSource.addItem(curve)

def main():
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

And finally, many IDEs do not handle error messages correctly, pointing out only an error code such as -1073740791 (0xC0000409) that they never help, the best thing in those cases is to execute your script in a CMD or console since they will give you a message more descriptive.

answered on Stack Overflow Oct 13, 2018 by eyllanesc • edited Oct 13, 2018 by eyllanesc

User contributions licensed under CC BY-SA 3.0