Multiple live plot GUI using pyqtgraph and PyQt5 - python

I am trying to create a gui with several live plot EEG/ECG graphs (each plot on different axes).
From what I understand, I need to create multiple PlotWidgets inside a grid layout.
I have created a live plot using pyqtgraph, below however I am unsure how to merge this into the GUI:
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import collections
import random
import time
import math
import numpy as np
class DynamicPlotter:
def __init__(self, sampleinterval=0.1, timewindow=10., size=(600, 350)):
# Data stuff
self.interval = int(sampleinterval * 1000)
self.bufsize = int(timewindow / sampleinterval)
self.databuffer = collections.deque([0.0] * self.bufsize, self.bufsize)
self.x = np.linspace(-timewindow, 0.0, self.bufsize)
self.y = np.zeros(self.bufsize, dtype=float)
# PyQtGraph stuff
self.app = QtGui.QApplication([])
self.plt = pg.plot(title='EEG/ECG Live Plot')
self.plt.resize(*size)
self.plt.showGrid(x=True, y=True)
#self.plt.setXRange(5,20, padding=0)
self.plt.setLabel('left', 'Amplitude', 'uVrms')
self.plt.setLabel('bottom', 'Time', 's')
self.curve = self.plt.plot(self.x, self.y, pen=(255, 0, 0))
# QTimer
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.updateplot)
self.timer.start(self.interval)
def getdata(self):
frequency = 0.5
noise = random.normalvariate(0., 1.)
new = 10. * math.sin(time.time() * frequency * 2 * math.pi) + noise
return new
def updateplot(self):
self.databuffer.append(self.getdata())
self.y[:] = self.databuffer
self.curve.setData(self.x, self.y)
self.app.processEvents()
def run(self):
self.app.exec_()
if __name__ == '__main__':
livePlot = DynamicPlotter(sampleinterval=0.05, timewindow=5.)
livePlot.run()
Here is the basic GUI (3 plot widgets inside grid, and a few labels in mainWindow):
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(845, 727)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.labelTitle = QtWidgets.QLabel(self.centralwidget)
self.labelTitle.setGeometry(QtCore.QRect(280, 0, 291, 51))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.labelTitle.setFont(font)
self.labelTitle.setObjectName("labelTitle")
self.labelCh1 = QtWidgets.QLabel(self.centralwidget)
self.labelCh1.setGeometry(QtCore.QRect(20, 90, 31, 51))
self.labelCh1.setObjectName("labelCh1")
self.labelCh2 = QtWidgets.QLabel(self.centralwidget)
self.labelCh2.setGeometry(QtCore.QRect(20, 180, 31, 51))
self.labelCh2.setObjectName("labelCh2")
self.labelCh3 = QtWidgets.QLabel(self.centralwidget)
self.labelCh3.setGeometry(QtCore.QRect(20, 260, 31, 51))
self.labelCh3.setObjectName("labelCh3")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(70, 70, 741, 261))
self.widget.setObjectName("widget")
self.gridLayout = QtWidgets.QGridLayout(self.widget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.ch1PlotWidget = PlotWidget(self.widget)
self.ch1PlotWidget.setObjectName("ch1PlotWidget")
self.gridLayout.addWidget(self.ch1PlotWidget, 0, 0, 1, 1)
self.ch2PlotWidget = PlotWidget(self.widget)
self.ch2PlotWidget.setObjectName("ch2PlotWidget")
self.gridLayout.addWidget(self.ch2PlotWidget, 1, 0, 1, 1)
self.ch3PlotWidget = PlotWidget(self.widget)
self.ch3PlotWidget.setObjectName("ch3PlotWidget")
self.gridLayout.addWidget(self.ch3PlotWidget, 2, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 845, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.labelTitle.setText(_translate("MainWindow", "EEG/ECG Recording GUI"))
self.labelCh1.setText(_translate("MainWindow", "Ch 1"))
self.labelCh2.setText(_translate("MainWindow", "Ch 2"))
self.labelCh3.setText(_translate("MainWindow", "Ch 3"))
from pyqtgraph import PlotWidget
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
My question is how do I integrate these two so I can plot the live graphs in each widget?
Ideally I want to use a super class so I can simply import the unedited gui.
I have tried importing the gui.Ui_MainWindow into the class and then overwriting the self.plt to self.Ch1PlotWidget
from pyqtgraph.Qt import QtGui, QtCore, QtWidgets
import gui as gui
import sys
import pyqtgraph as pg
import collections
import random
import time
import math
import numpy as np
class MainWindow(QtWidgets.QMainWindow, gui.Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
self.setupUi(self)
# Data stuff
self.interval = 100
self.bufsize = int(10 / self.interval)
self.databuffer = collections.deque([0.0] * self.bufsize, self.bufsize)
self.x = np.linspace(-10, 0.0, self.bufsize)
self.y = np.zeros(self.bufsize, dtype=float)
# PyQtGraph stuff
self.app = QtGui.QApplication([])
self.ch1PlotWidget = pg.plot(title='Live Plot')
self.ch1PlotWidget.resize(600, 350)
self.ch1PlotWidget.showGrid(x=True, y=True)
# self.plt.setXRange(5,20, padding=0)
self.ch1PlotWidget.setLabel('left', 'Amplitude', 'uVrms')
self.ch1PlotWidget.setLabel('bottom', 'Time', 's')
self.curve = self.ch1PlotWidget.plot(self.x, self.y, pen=(255, 0, 0))
# QTimer
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.updateplot)
self.timer.start(self.interval)
def getdata(self):
frequency = 0.5
noise = random.normalvariate(0., 1.)
new = 10. * math.sin(time.time() * frequency * 2 * math.pi) + noise
return new
def updateplot(self):
self.databuffer.append(self.getdata())
self.y[:] = self.databuffer
self.curve.setData(self.x, self.y)
self.app.processEvents()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Sorry for all the code, I am just very confused on how to implement logic to the gui.

Here is an option where you can use both classes with minimal changes.
Change the DynamicPlotter constructor to accept a PlotWidget as a argument instead of creating a new one, since they are created and added to a layout in Ui_Mainwindow. In the MainWindow class, create a DynamicPlotter object for each plot (and keep a persistent reference, in this case I added them to a list self.plots).
class DynamicPlotter:
def __init__(self, plot, sampleinterval=0.1, timewindow=10., size=(600, 350)):
# Data stuff
self.interval = int(sampleinterval * 1000)
self.bufsize = int(timewindow / sampleinterval)
self.databuffer = collections.deque([0.0] * self.bufsize, self.bufsize)
self.x = np.linspace(-timewindow, 0.0, self.bufsize)
self.y = np.zeros(self.bufsize, dtype=float)
# PyQtGraph stuff
self.plt = plot
self.plt.setTitle('EEG/ECG Live Plot')
self.plt.resize(*size)
self.plt.showGrid(x=True, y=True)
#self.plt.setXRange(5,20, padding=0)
self.plt.setLabel('left', 'Amplitude', 'uVrms')
self.plt.setLabel('bottom', 'Time', 's')
self.curve = self.plt.plot(self.x, self.y, pen=(255, 0, 0))
# QTimer
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.updateplot)
self.timer.start(self.interval)
def getdata(self):
frequency = 0.5
noise = random.normalvariate(0., 1.)
new = 10. * math.sin(time.time() * frequency * 2 * math.pi) + noise
return new
def updateplot(self):
self.databuffer.append(self.getdata())
self.y[:] = self.databuffer
self.curve.setData(self.x, self.y)
class MainWindow(QtWidgets.QMainWindow, gui.Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
self.setupUi(self)
self.plots = []
for plot in (self.ch1PlotWidget, self.ch2PlotWidget, self.ch3PlotWidget):
self.plots.append(
DynamicPlotter(plot, sampleinterval=0.05, timewindow=5.)
)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

Related

OpenGL Canvas. how to move an object inside a canvas

I want to move the following red cross in the canvas with the mouse events. it should only move when we click on it and drag it with the move. it should stop moving when we release the mouse.
I do get the events of the mouse. but I don't know how i can detect that I clicked on the object to make it move.
also for example i can't set self.plot1.pos to change its position. we don't have access to that attribute.
does anybody have an idea?
I am using python 3.5 and OpenGL Canvas with vispy and a QtWidgets window.
import sys
from PySide2 import QtWidgets
from vispy import scene
from PySide2.QtCore import QMetaObject
from PySide2.QtWidgets import *
import numpy as np
class my_canvas(scene.SceneCanvas):
def __init__(self):
super().__init__(keys="interactive")
self.unfreeze()
self.view = self.central_widget.add_view()
self.view.bgcolor = '#ffffff' # set the canva to a white background
window_size_0 = 800, 400
window_center = window_size_0[0] / 2, window_size_0[1] / 2
crosshair_max_length = 50
data_1 = np.random.normal(size=(2, 2))
data_1[0] = window_center[0] - crosshair_max_length, window_center[1]
data_1[1] = window_center[0] + crosshair_max_length, window_center[1]
data_2 = np.random.normal(size=(2, 2))
data_2[0] = window_center[0], window_center[1] - crosshair_max_length
data_2[1] = window_center[0], window_center[1] + crosshair_max_length
self.plot1 = scene.Line(data_1, parent=self.view.scene, color="r")
self.plot2 = scene.Line(data_2, parent=self.view.scene, color="r")
self.selected_object = None
self.freeze()
def on_mouse_press(self, event):
if event.button == 1:
print("pressed left")
if event.button == 2:
print("pressed right")
def on_mouse_move(self, event):
pass
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName("MainWindow")
MainWindow.resize(748, 537)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.groupBox = QGroupBox(self.centralwidget)
self.groupBox.setObjectName("groupBox")
self.gridLayout.addWidget(self.groupBox, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
QMetaObject.connectSlotsByName(MainWindow)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# OpenGL drawing surface
self.canvas = my_canvas()
self.canvas.create_native()
self.canvas.native.setParent(self)
self.setWindowTitle('MyApp')
def main():
import ctypes
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('my_gui')
app = QtWidgets.QApplication([])
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
i found the solution for the ones who are interested.
self.plot1.set_data(pos=...)
with this method we can move it easily

The graph is not drawn in the matplotlib

I'm drawing a graph in the matplotlib by converting to FFT with Arduino data. However, the same error does not cause a graph. I think it's because of self in update() what should I do? I don't know how to change when there is a Typeerror in my code. If you find a problem, can you tell me?
+ I solved the error. However, the graph is not printed. The code has been modified as a whole. What happen?
from PyQt5.QtWidgets import*
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import random
from PyQt5 import QtCore, QtGui, QtWidgets
import datetime
import serial
import time
import random
import numpy as np
from matplotlib import animation
from collections import deque # import a "circular" list
from threading import Thread, Lock
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1212, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.widget = matplotlibWidget()
self.widget.setObjectName("widget")
self.gridLayout.addWidget(self.widget, 0, 0, 1, 1)
self.centralwidget.setLayout(self.gridLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
#from matplotlibwidgetFile import matplotlibWidget
class matplotlibWidget(QWidget):
def __init__(self, parent = None):
QWidget.__init__(self,parent)
self.i = 0
self.data = []
lock = Lock()
freq = 2000 # 1/T
self.guarda = 100 # 200
r = range(0, int(freq/2+1), int(freq/self.guarda))
self.frequencia = np.fft.fftfreq(self.guarda, d=1/freq)
self.acelx = deque([], maxlen=self.guarda)
self.fig = plt.figure()
self.ax = self.fig.add_subplot(111)
self.canvas = FigureCanvas(self.fig)
self.right_layout = QVBoxLayout()
self.right_layout.addWidget(self.canvas)
self.setLayout(self.right_layout)
self.ax.set_xlim((0,int(freq/2)))
self.ax.set_ylim((0,1000))
self.line, = self.ax.plot([],[])
self.ax.grid(True)
def data_input():
for line in arduinoData:
try:
self.i+=1
self.acelx.append(float(line))
with lock:
if self.i > len(self.acelx):
self.data = np.fft.fft(self.acelx)
except ValueError:
pass
t = Thread(target=data_input)
t.daemon = True
t.start()
S = Scope(self.line,self.frequencia, self.guarda, self.data, self.ax, self.acelx, self.i )
timer = QtCore.QTimer()
timer.timeout.connect(S.update)
timer.start(0)
class Scope(matplotlibWidget) :
def __init__(self, line, frequencia, guarda, data, ax, acelx, i) :
self.line, = line,
self.ax = ax
self.frequencia = frequencia
self.guarda = guarda
self.data = data
self.acelx = acelx
self.i = i
def update(self) :
if self.i > len(self.acelx) :
self.line.set_data(self.frequencia[:int(self.guarda/2)], abs(np.real(self.data[:int(self.guarda/2)])))
self.ax.figure.canvas.draw()
return (self.line,)
if __name__ == "__main__":
import sys
value = [0,0,0,0,0,0,0,0,0,0]
arduinoData = serial.Serial('com5', 9600)
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
You have to create instance:
s = Scope()
timer = QtCore.QTimer()
timer.timeout.connect(s.update)
or
timer = QtCore.QTimer()
timer.timeout.connect(Scope().update)

Transfer data from a scatterplot to a lineEdit - Matplotlib and Pyqt5

I have GUI developed with PyQt5 with an interactive scatterplot in a widget and 2 lineEdit. By clicking a dot, I can get the x and y values but I can't populate my two lineEdit with the two values.
Front_end code:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(539, 400)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.lineEdit.sizePolicy().hasHeightForWidth())
self.lineEdit.setSizePolicy(sizePolicy)
self.lineEdit.setObjectName("lineEdit")
self.gridLayout.addWidget(self.lineEdit, 0, 0, 1, 1)
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setObjectName("widget")
self.gridLayout.addWidget(self.widget, 0, 1, 2, 1)
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.lineEdit_2.sizePolicy().hasHeightForWidth())
self.lineEdit_2.setSizePolicy(sizePolicy)
self.lineEdit_2.setObjectName("lineEdit_2")
self.gridLayout.addWidget(self.lineEdit_2, 1, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Back_end code:
import sys
import matplotlib.pyplot as plt
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QSizePolicy
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from front_test import Ui_MainWindow
class Graph_init(FigureCanvas):
def __init__(self, parent=None):
fig = Figure()
fig.patch.set_facecolor("None")
self.axes = fig.add_subplot(111)
self.compute_initial_figure()
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
FigureCanvas.mpl_connect(self, 'pick_event', self.onclick)
def onclick(self, event):
ind = event.ind[0]
data = event.artist.get_offsets()
xdata, ydata = data[ind,:]
print ((xdata, ydata))
index = [10,20,30].index(xdata)
x = [10,20,30]
y = [100,100,150]
size = [1000,2000,3000]
x_high = x[index]
y_high = y[index]
size_high = size[index]
del x[index]
del y[index]
del size[index]
self.axes.clear()
self.axes.scatter(x, y, s=size, color='blue', picker=1, alpha=0.3)
self.axes.scatter(x_high, y_high, s=size_high, color='red', picker=1, alpha=0.3)
self.axes.figure.canvas.draw()
#self.lineEdit.setText(xdata)
#self.lineEdit_2.setText(ydata)
class Graph_populate(Graph_init):
def compute_initial_figure(self):
x = [10,20,30]
y = [100,100,150]
size = [1000,2000,3000]
self.axes.scatter(x,y,s=size,color='blue', picker=1, alpha=0.3)
class GUI(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(GUI, self).__init__(parent)
self.setupUi(self)
self.sc = Graph_populate(self.widget)
self.gridLayout.addWidget(self.sc, 0, 1, 2, 1)
if __name__ == '__main__':
app = QApplication(sys.argv)
prog = GUI()
prog.showMaximized()
sys.exit(app.exec_())
Thank you
You can not update the QLineEdits in Graph_init since they do not exist in that context, in those cases it is advisable to send the information where the context exists through a signal, this signal must be created:
posClicked = QtCore.pyqtSignal(tuple)
And when you want to send emit() is used
self.posClicked.emit((xdata, ydata))
Then it must be connected to a slot where it is used to fill the QLineEdits as shown below:
class Graph_init(FigureCanvas):
posClicked = QtCore.pyqtSignal(tuple)
def __init__(self, parent=None):
[...]
def onclick(self, event):
ind = event.ind[0]
data = event.artist.get_offsets()
xdata, ydata = data[ind,:]
print ((xdata, ydata))
[...]
self.axes.figure.canvas.draw()
self.posClicked.emit((xdata, ydata))
class GUI(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(GUI, self).__init__(parent)
self.setupUi(self)
self.sc = Graph_populate(self.widget)
self.gridLayout.addWidget(self.sc, 0, 1, 2, 1)
self.sc.posClicked.connect(self.onPosClicked)
def onPosClicked(self, values):
xdata, ydata = values
self.lineEdit.setText(str(xdata))
self.lineEdit_2.setText(str(ydata))

How can I draw inside existing QGraphicsVIew?

just like the title says i'm trying to draw inside an existing QGraphicsView. The window I generated using QT Designer, and I would like to draw a matrix of 16x16 squares inside the QGraphicsView.
This is the code I have as of right now:
Window:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.8.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(911, 567)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 10, 301, 31))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(620, 40, 271, 91))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.groupBox.setFont(font)
self.groupBox.setObjectName("groupBox")
self.rdtest = QtWidgets.QRadioButton(self.groupBox)
self.rdtest.setGeometry(QtCore.QRect(60, 60, 101, 22))
self.rdtest.setObjectName("rdtest")
self.rdtrain = QtWidgets.QRadioButton(self.groupBox)
self.rdtrain.setGeometry(QtCore.QRect(60, 30, 101, 22))
self.rdtrain.setObjectName("rdtrain")
self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
self.graphicsView.setGeometry(QtCore.QRect(10, 40, 601, 411))
self.graphicsView.setObjectName("graphicsView")
self.clear = QtWidgets.QPushButton(self.centralwidget)
self.clear.setGeometry(QtCore.QRect(10, 470, 88, 29))
self.clear.setObjectName("clear")
self.trainNewInput = QtWidgets.QPushButton(self.centralwidget)
self.trainNewInput.setGeometry(QtCore.QRect(500, 470, 111, 29))
self.trainNewInput.setObjectName("trainNewInput")
self.trainbox = QtWidgets.QGroupBox(self.centralwidget)
self.trainbox.setGeometry(QtCore.QRect(620, 150, 271, 171))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.trainbox.setFont(font)
self.trainbox.setObjectName("trainbox")
self.vtrain = QtWidgets.QLineEdit(self.trainbox)
self.vtrain.setGeometry(QtCore.QRect(100, 70, 101, 29))
self.vtrain.setObjectName("vtrain")
self.label_2 = QtWidgets.QLabel(self.trainbox)
self.label_2.setGeometry(QtCore.QRect(40, 70, 56, 31))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.trainDefault = QtWidgets.QPushButton(self.trainbox)
self.trainDefault.setGeometry(QtCore.QRect(40, 30, 161, 29))
self.trainDefault.setObjectName("trainDefault")
self.output = QtWidgets.QLabel(self.trainbox)
self.output.setGeometry(QtCore.QRect(10, 130, 271, 17))
self.output.setText("")
self.output.setObjectName("output")
self.output_2 = QtWidgets.QLabel(self.trainbox)
self.output_2.setGeometry(QtCore.QRect(10, 150, 271, 17))
self.output_2.setText("")
self.output_2.setObjectName("output_2")
self.testbox = QtWidgets.QGroupBox(self.centralwidget)
self.testbox.setGeometry(QtCore.QRect(620, 330, 271, 171))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.testbox.setFont(font)
self.testbox.setObjectName("testbox")
self.pushButton = QtWidgets.QPushButton(self.testbox)
self.pushButton.setGeometry(QtCore.QRect(50, 40, 161, 29))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 911, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "RNA: Numeros "))
self.groupBox.setTitle(_translate("MainWindow", "Opciones:"))
self.rdtest.setText(_translate("MainWindow", "Test"))
self.rdtrain.setText(_translate("MainWindow", "Train"))
self.clear.setText(_translate("MainWindow", "Clear"))
self.trainNewInput.setText(_translate("MainWindow", "Train new Input"))
self.trainbox.setTitle(_translate("MainWindow", "Train:"))
self.label_2.setText(_translate("MainWindow", "% Test:"))
self.trainDefault.setText(_translate("MainWindow", "Train Default Data"))
self.testbox.setTitle(_translate("MainWindow", "Test:"))
self.pushButton.setText(_translate("MainWindow", "Start Test"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Logic class where I want to create a method that draws on the QGraphicsView from the window:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QGraphicsScene
from PyQt5.QtCore import (QLineF, QPointF, QRectF, Qt)
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtWidgets import (QApplication, QGraphicsView, QGraphicsScene, QGraphicsItem,
QGridLayout, QVBoxLayout, QHBoxLayout,
QLabel, QLineEdit, QPushButton)
from window import Ui_MainWindow
from RNA import NumberIA
train = []
train_r = []
test = []
test_r = []
class MyFirstGuiProgram(Ui_MainWindow):
def __init__(self, dialog):
Ui_MainWindow.__init__(self)
self.setupUi(dialog)
self.rdtrain.setChecked(True)
# Connect "add" button with a custom function (addInputTextToListbox)
self.clear.clicked.connect(self.addInputTextToListbox)
self.trainDefault.clicked.connect(self.t_DefaultData)
self.rdtest.clicked.connect(self.enableTest)
self.rdtrain.clicked.connect(self.enableTrain)
def enableTest(self):
self.trainbox.setEnabled(False)
self.trainNewInput.setEnabled(False)
self.testbox.setEnabled(True)
def drawSomething(self):
tes = "f"
def enableTrain(self):
self.trainbox.setEnabled(True)
self.trainNewInput.setEnabled(True)
self.testbox.setEnabled(False)
def addInputTextToListbox(self):
print("Hello world!!!")
def t_DefaultData(self):
ann = NumberIA()
t = self.vtrain.text()
print("Training data: ", t)
global train
global train_r
global test
global test_r
train, train_r, test, test_r = ann.read_file(float(t))
self.output.setText("Train complete...")
ann.crear_red(train, train_r)
accuracy, matrix = ann.probar_red(test, test_r)
self.output_2.setText("Accuracy: " + str(accuracy * 100) + "%")
print("Matris de Confusion:")
print('\n'.join([''.join(['{:4}'.format(item) for item in row])
for row in matrix]))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = MyFirstGuiProgram(MainWindow)
MainWindow.show()
MainWindow.setWindowTitle("RNA: Inteligencia Artificial 2")
ui.enableTrain()
sys.exit(app.exec_())
I cant seem to find examples on how to to this online, the examples I have found are vague to me and cant seem to figure it out.
I recommend you inherit from QMainWindow and implement the Ui_MainWindow interface, to draw in a QGraphicsView you must use a QGraphicsScene, and use its methods, in this case use addRect().
from PyQt5 import QtCore, QtGui, QtWidgets
class MyFirstGuiProgram(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent=parent)
self.setupUi(self)
scene = QtWidgets.QGraphicsScene()
self.graphicsView.setScene(scene)
pen = QtGui.QPen(QtCore.Qt.green)
side = 20
for i in range(16):
for j in range(16):
r = QtCore.QRectF(QtCore.QPointF(i*side, j*side), QtCore.QSizeF(side, side))
scene.addRect(r, pen)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MyFirstGuiProgram()
w.show()
sys.exit(app.exec_())
You can also draw it by QLineF(), it gives more control from design point of view.
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
def sceneWithPen(grid):
scene = QGraphicsScene()
view = QGraphicsView()
view.setScene(scene)
# Creating line's colors
red = QColor(qRgb(172, 50, 99))
blue = QColor(qRgb(50, 150, 203))
# Set length of square's side and number of horizontal and vertical lines
vLines = 16
hLines = 16
side = 30
# Set starting values for loops
hor = 0
ver = 0
subdiv = 16
leftX,leftY = 0, 0
rightX, rightY = subdiv*side, 0
bottomX, bottomY= 0, 0
topX, topY = 0, subdiv*side
while ver < vLines:
# Drawing vertical lines
ver = ver + 1
vLine = QLineF(bottomX, bottomY, topX, topY)
bottomX, topX = bottomX + side, topX + side
scene.addLine(vLine, red)
while hor < hLines:
#Drawing horizontal lines
hor = hor + 1
hLine = QLineF(leftX, leftY, rightX, rightY)
leftY, rightY = leftY + side, rightY + side
scene.addLine(hLine, blue)
grid.addWidget(view)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = QWidget()
grid = QGridLayout()
sceneWithPen(grid)
w.setLayout(grid)
w.show()
sys.exit(app.exec_())
It will be like this:
Great example #eyllanesc just gave you right above, here is another small example I just happened to have laying around, if you need to draw like a simple free hand.
Have your MainWindow, for example a QMainWindow or Qwidget or...
Have a QGraphicsView.
Set the QGraphicsView's scene with your QGraphicsScene
Have your own QGraphicsPathItem so you can set properties you need.
Have the implementation of what you want to draw inside your scene, so it knows how to draw on itself.
Having things in this way you will have control of several scenes knowing how to draw on itself independently, of course if you will need more than one scene.
This example the logic were implemented inside the QGraphicsView, you could move it to your GraphicsScene.
class MainWindow(QMainWindow):
central_widget = None
layout_container = None
def __init__(self):
super(MainWindow, self).__init__()
self.central_widget = QWidget()
self.layout_container = QVBoxLayout()
self.central_widget.setLayout(self.layout_container)
self.setCentralWidget(self.central_widget)
self.layout_container.addWidget(GraphicsView())
class GraphicsView(QGraphicsView):
start = None
end = None
item = None
path = None
def __init__(self):
super(GraphicsView, self).__init__()
self.setScene(QGraphicsScene())
self.path = QPainterPath()
self.item = GraphicsPathItem()
self.scene().addItem(self.item)
def mousePressEvent(self, event):
self.start = self.mapToScene(event.pos())
self.path.moveTo(self.start)
def mouseMoveEvent(self, event):
self.end = self.mapToScene(event.pos())
self.path.lineTo(self.end)
self.start = self.end
self.item.setPath(self.path)
class GraphicsPathItem(QGraphicsPathItem):
def __init__(self):
super(GraphicsPathItem, self).__init__()
pen = QPen()
pen.setColor(Qt.black)
pen.setWidth(10)
self.setPen(pen)
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())

Deleting and Redrawing Matplotlib Animation

There are a few parts to this problem, so let me summarize. I'm trying to be able to create an animation in a PySide window, and then later destroy and reanimate it based on new information. I've created a sample script to demonstrate this, but it's a bit long, so here's an outline:
Create a QtGui main window using PySide
Create a widget in the window for drawing the animation
Animate the widget using FuncAnimation
On a button press:
Delete the widget and animation
Recreate the widget using different parameters for plotting
Reanimate
Everything works until the button press, and I get the following stack trace:
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_qt4.py", line 366, in idle_draw
self.draw()
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_qt4agg.py", line 149, in draw
self.update()
RuntimeError: Internal C++ object (MplWidget) already deleted.
How can I get rid of the current animation so that I can redraw a separate one? Here's the code.
from matplotlib import pyplot as p
from mpl_toolkits.mplot3d import Axes3D # #UnusedImport
from PySide import QtGui, QtCore
import matplotlib
import matplotlib.animation as animation
import sys
# specify the use of PySide
matplotlib.rcParams['backend.qt4'] = "PySide"
# import the figure canvas for interfacing with the backend
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg \
as FigureCanvas
from matplotlib.figure import Figure
import numpy as np
from math import pi, cos, sin
class Ui_MainWindow(object):
def setupUi(self, MainWindow, direction, maxRadius):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 500)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton = QtGui.QPushButton(self.centralwidget)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Maximum,
QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(
self.pushButton.sizePolicy().hasHeightForWidth())
self.pushButton.setSizePolicy(sizePolicy)
self.pushButton.setMaximumSize(QtCore.QSize(150, 16777215))
self.pushButton.setObjectName("pushButton")
self.pushButton.setText("Change Direction")
self.horizontalLayout.addWidget(self.pushButton)
self.frame = QtGui.QFrame(self.centralwidget)
self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame.setFrameShadow(QtGui.QFrame.Raised)
self.frame.setObjectName("frame")
self.gridLayout = QtGui.QGridLayout(self.frame)
self.gridLayout.setObjectName("gridLayout")
self.horizontalLayout.addWidget(self.frame)
# ------
self.mplWidget = MplWidget(self.frame, direction, maxRadius)
# ------
MainWindow.setCentralWidget(self.centralwidget)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.direction = 'up'
self.maxRadius = 0.3
self.ui.setupUi(self, self.direction, self.maxRadius)
self.ui.pushButton.clicked.connect(self.changeStuff)
self.animation = self.ui.mplWidget.animate()
def changeStuff(self):
del self.animation
self.ui.mplWidget.deleteLater()
dirs = {'up' :'down', 'down':'up'}
rads = {0.3:1, 1:0.3}
self.direction = dirs[self.direction]
self.maxRadius = rads[self.maxRadius]
self.ui.mplWidget = MplWidget(self.ui.frame, self.direction,
self.maxRadius)
self.animation = self.ui.mplWidget.animate()
class MplWidget(FigureCanvas):
def __init__(self, parent=None, direction='up', maxRadius=0.3):
self.figure = Figure()
super(MplWidget, self).__init__(self.figure)
self.setParent(parent)
self.axes = self.figure.add_subplot(111, projection='3d')
self.axes.set_xlabel("x label")
self.axes.set_ylabel("y label")
self.axes.set_zlabel("z label")
self.axes.set_xlim3d([-1, 1])
self.axes.set_ylim3d([-1, 1])
self.axes.set_zlim3d([-1, 1])
self.axes.set_aspect('equal')
if direction == 'up':
self.c = 1
elif direction == 'down':
self.c = -1
else:
self.c = 1
self.maxRadius = maxRadius
self.frames = 50
self.plot_handle = self.func_plot(self.frames)
def func_plot(self, z):
z /= float(self.frames) * self.c
theta = np.arange(0, 2 * pi + pi / 50, pi / 50)
xdata = self.maxRadius * z * np.array([cos(q) for q in theta])
ydata = self.maxRadius * z * np.array([sin(q) for q in theta])
zdata = z * np.ones(np.shape(xdata))
if not hasattr(self, 'plot_handle'):
plot_handle = self.axes.plot(xdata, ydata, zdata)[0]
else:
plot_handle = self.plot_handle
plot_handle.set_data(xdata, ydata)
plot_handle.set_3d_properties(zdata)
return plot_handle
def animate(self):
return animation.FuncAnimation(
fig=self.figure, func=self.func_plot, frames=self.frames,
interval=1000.0 / self.frames, blit=False)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
Version information:
matplotlib - 1.2.1
PySide - 1.1.2
Python - 2.7.4
Edit:
I followed the matplotlib Installation Instructions to do a source build from git. I now have version 1.4.x installed. However, the original problem persists.
This is related to a bug in matplotlib qtbackend that tears things down in the wrong order (see here). Adding
self.ui.mplWidget.close_event()
in ChangeStuff takes care of the exception you have, but the newly created canvas isn't shown.
On the other hand, I don't understand why any canvas is shown at all as the widget is never added to a layout. If you tweak the set up a bit and explicitly add/remove the widget to the gridLayout it does what you want:
from mpl_toolkits.mplot3d import Axes3D # #UnusedImport
from PySide import QtGui, QtCore
import matplotlib
import matplotlib.animation as animation
import sys
# specify the use of PySide
matplotlib.rcParams['backend.qt4'] = "PySide"
# import the figure canvas for interfacing with the backend
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg \
as FigureCanvas
from matplotlib.figure import Figure
import numpy as np
from math import pi, cos, sin
class Ui_MainWindow(object):
def setupUi(self, MainWindow, direction, maxRadius):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 500)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton = QtGui.QPushButton(self.centralwidget)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Maximum,
QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(
self.pushButton.sizePolicy().hasHeightForWidth())
self.pushButton.setSizePolicy(sizePolicy)
self.pushButton.setMaximumSize(QtCore.QSize(150, 16777215))
self.pushButton.setObjectName("pushButton")
self.pushButton.setText("Change Direction")
self.horizontalLayout.addWidget(self.pushButton)
self.frame = QtGui.QFrame(self.centralwidget)
self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame.setFrameShadow(QtGui.QFrame.Raised)
self.frame.setObjectName("frame")
self.gridLayout = QtGui.QGridLayout(self.frame)
self.gridLayout.setObjectName("gridLayout")
self.horizontalLayout.addWidget(self.frame)
# ------
self.mplWidget = MplWidget(None, direction, maxRadius)
self.gridLayout.addWidget(self.mplWidget)
# ------
MainWindow.setCentralWidget(self.centralwidget)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.direction = 'up'
self.maxRadius = 0.3
self.ui.setupUi(self, self.direction, self.maxRadius)
self.ui.pushButton.clicked.connect(self.changeStuff)
self.animation = self.ui.mplWidget.animate()
def changeStuff(self):
self.ui.mplWidget.close_event() # mpl clean up
self.ui.mplWidget.deleteLater() # QT cleanup
self.ui.gridLayout.removeWidget(self.ui.mplWidget)
dirs = {'up': 'down', 'down': 'up'}
rads = {0.3: 1, 1: 0.3}
self.direction = dirs[self.direction]
self.maxRadius = rads[self.maxRadius]
self.ui.mplWidget = MplWidget(self.ui.frame, self.direction,
self.maxRadius)
self.ui.gridLayout.addWidget(self.ui.mplWidget)
self.animation = self.ui.mplWidget.animate()
print self.ui.frame.children()
print 'finished change stuff'
class MplWidget(FigureCanvas):
def __init__(self, parent=None, direction='up', maxRadius=0.3):
self.figure = Figure()
super(MplWidget, self).__init__(self.figure)
self.setParent(parent)
self.axes = self.figure.add_subplot(111, projection='3d')
self.axes.set_xlabel("x label")
self.axes.set_ylabel("y label")
self.axes.set_zlabel("z label")
self.axes.set_xlim3d([-1, 1])
self.axes.set_ylim3d([-1, 1])
self.axes.set_zlim3d([-1, 1])
self.axes.set_aspect('equal')
if direction == 'up':
self.c = 1
elif direction == 'down':
self.c = -1
else:
self.c = 1
self.maxRadius = maxRadius
self.frames = 50
self.plot_handle = self.func_plot(self.frames)
def func_plot(self, z):
z /= float(self.frames) * self.c
theta = np.arange(0, 2 * pi + pi / 50, pi / 50)
xdata = self.maxRadius * z * np.array([cos(q) for q in theta])
ydata = self.maxRadius * z * np.array([sin(q) for q in theta])
zdata = z * np.ones(np.shape(xdata))
if not hasattr(self, 'plot_handle'):
plot_handle = self.axes.plot(xdata, ydata, zdata)[0]
else:
plot_handle = self.plot_handle
plot_handle.set_data(xdata, ydata)
plot_handle.set_3d_properties(zdata)
return plot_handle
def animate(self):
return animation.FuncAnimation(
fig=self.figure, func=self.func_plot, frames=self.frames,
interval=1000.0 / self.frames, blit=False)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())

Categories

Resources