Create several popup plots with FigureCanvas in PyQt4 - python

Following code creates a Widget with a Button and a Progressbar. When Button is pressed and Progressbar reaches 100%, 3 plots are shown (by Matplotlib.pyplot):
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import matplotlib.pyplot as plt
import pandas as pd
class App(QMainWindow):
def __init__(self):
super(App, self).__init__()
self.setGeometry(500, 300, 820, 350)
self.setWindowTitle("Program")
#Buttons
btnposx = 30
btnposy = 50
self.btn4 = QPushButton('Load', self)
self.btn4.move(btnposx,btnposy+220)
self.btn4.released.connect(self.thread)
#ProgressBar
self.pb = QProgressBar(self)
self.pb.move(btnposx+150,btnposy+220)
self.pb.resize(470,27)
self.show()
#pyqtSlot(float)
def load(self, val):
self.pb.setValue(val)
#pyqtSlot(object)
def plot(self, pq):
pq.plot(grid = 1)
plt.show()
def thread(self):
self.thread_ = Thread()
self.thread_.pb_signal.connect(self.load, Qt.QueuedConnection)
self.thread_.plot_signal.connect(self.plot, Qt.QueuedConnection)
self.thread_.start()
class Thread(QThread):
pb_signal = pyqtSignal(float)
plot_signal = pyqtSignal(object)
def __init__(self, *args, **kwargs):
QThread.__init__(self, *args, **kwargs)
def __del__(self):
self.wait()
#pyqtSlot()
def run(self):
val = 0
self.pb_signal.emit(20)
l = range(50000000)
for i in l:
val += 1
self.pb_signal.emit(60)
self.pb_signal.emit(100)
pq = pd.DataFrame(data = {'col1':[1,2,3,4,5,6], 'col2':[6,5,4,3,2,1]})
self.plot_signal.emit(pq)
self.plot_signal.emit(pq)
self.plot_signal.emit(pq)
return
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())
How do I do the exact same Thing with FigureCanvas? I don't want to make one window, in that 3 plots are embedded but 3 seperate figures. The Information online about how to use FigureCanvas this way is very scarce.

FigureCanvas is a specialized QWidget that can contain a Figure of matplotlib, and in that Figure you can make the plots, so the solution is to create one for each emission, in addition to that the plotting functions have an additional argument that receives the axes where it will be drawn, so you must pass the axes created inside Figure:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import pandas as pd
from matplotlib.backends.backend_qt4agg import (
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure
class App(QMainWindow):
def __init__(self):
super(App, self).__init__()
self.setGeometry(500, 300, 820, 350)
self.setWindowTitle("Program")
#Buttons
btnposx = 30
btnposy = 50
self.btn4 = QPushButton('Load', self)
self.btn4.move(btnposx,btnposy+220)
self.btn4.released.connect(self.thread)
#ProgressBar
self.pb = QProgressBar(self)
self.pb.move(btnposx+150,btnposy+220)
self.pb.resize(470,27)
self.canvas = []
self.show()
#pyqtSlot(float)
def load(self, val):
self.pb.setValue(val)
#pyqtSlot(object)
def plot(self, pq):
cv = FigureCanvas(Figure(figsize=(5, 3)))
ax = cv.figure.subplots()
pq.plot(grid = 1, ax=ax)
cv.show()
# avoid garbage collector
self.canvas.append(cv)
def thread(self):
self.thread_ = Thread()
self.thread_.pb_signal.connect(self.load, Qt.QueuedConnection)
self.thread_.plot_signal.connect(self.plot, Qt.QueuedConnection)
self.thread_.start()
class Thread(QThread):
pb_signal = pyqtSignal(float)
plot_signal = pyqtSignal(object)
def __del__(self):
self.wait()
#pyqtSlot()
def run(self):
val = 0
self.pb_signal.emit(20)
l = range(50000000)
for i in l:
val += 1
self.pb_signal.emit(60)
self.pb_signal.emit(100)
pq = pd.DataFrame(data = {'col1':[1,2,3,4,5,6], 'col2':[6,5,4,3,2,1]})
self.plot_signal.emit(pq)
self.plot_signal.emit(pq)
self.plot_signal.emit(pq)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())
Note: only the list has been created to prevent the garbage collector from deleting the plots

Related

Method is still running after closing window

I'm having trouble trying to undersand how the window objects work. I have a real time plot on a secundary window via matplotlib. This plot updates every second with the new calculated values.
What I expect is the method to stop running whenever I close this 2nd window and restarts when I reopen it. I'm able to reset the array values when I'm closing the 2nd window but as indicated, this method calculating the values is still running.
This is my code (originally using a sensor, I modified this to generate random numbers instead):
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
import sys
import random
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
# The main window
class Window(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(200,200, 400,300)
self.setWindowTitle('Test')
self.demo = None
self.create_buttons()
def create_buttons(self):
btn1 = QPushButton('Open window', self)
btn1.setGeometry(100,100, 100,100)
btn1.clicked.connect(self.open_w)
def open_w(self):
if self.demo is None:
self.demo = AppDemo()
self.demo.show()
# Second window, where I plot the data
class AppDemo(QWidget):
def __init__(self):
super().__init__()
self.resize(650, 500)
self.plotwidget = QWidget(self)
self.chart = Matplotlibwidget()
self.chart.plot_widget(self.plotwidget, self.chart)
def closeEvent(self, event):
event.accept()
func.start_lists()
print('Window closed')
# My widget for my graph
class Matplotlibwidget(FigureCanvas):
def __init__(self, parent=None):
fig = Figure()
self.axes = fig.add_subplot(111)
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.updateGeometry(self)
def plot_widget(self, canvasWidget, graph):
self.layoutvertical = QVBoxLayout(canvasWidget)
self.layoutvertical.addWidget(graph)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.plotting)
timer.start(1000)
def plotting(self):
self.y = func.calculate()
func.appends(self.y)
self.axes.cla()
self.axes.plot(func.x, func.y)
self.draw()
# This is a generic class I use to calculate the data, originally this is a class for my sensor
class FuncTime ():
def __init__(self):
self.start_lists()
def start_lists(self):
self.x, self.y = [0], [1]
def calculate(self):
return self.y[-1] + self.y[-1] * random.uniform(-0.1, 0.1)
def appends(self, y):
print(self.x[-1], ', ', self.y[-1])
self.x.append(self.x[-1] + 1)
self.y.append(y)
app = QApplication(sys.argv)
func = FuncTime()
window = Window()
window.show()
sys.exit(app.exec_())
That the window closes does not imply that the timer stops, you have to implement it using the stop method of the QTimer.
from functools import cached_property
import random
import sys
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class Window(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(200, 200, 400, 300)
self.setWindowTitle("Test")
self.create_buttons()
#cached_property
def demo(self):
return AppDemo()
def create_buttons(self):
btn1 = QPushButton("Open window", self)
btn1.setGeometry(100, 100, 100, 100)
btn1.clicked.connect(self.open_w)
def open_w(self):
self.demo.chart.start()
self.demo.show()
class AppDemo(QWidget):
def __init__(self):
super().__init__()
self.resize(650, 500)
self.chart = Matplotlibwidget()
lay = QVBoxLayout(self)
lay.addWidget(self.chart)
def closeEvent(self, event):
super().closeEvent(event)
self.chart.stop()
class Matplotlibwidget(FigureCanvas):
def __init__(self, parent=None):
fig = Figure()
self.axes = fig.add_subplot(111)
FigureCanvas.__init__(self, fig)
self.setParent(parent)
self.updateGeometry()
#cached_property
def timer(self):
return QTimer(interval=1000, timeout=self.handle_timeout)
#cached_property
def func_time(self):
return FuncTime()
def start(self):
self.timer.start()
def stop(self):
self.timer.stop()
self.func_time.reset()
def handle_timeout(self):
self.plotting()
def plotting(self):
self.y = self.func_time.calculate()
self.func_time.appends(self.y)
self.axes.cla()
self.axes.plot(self.func_time.x, self.func_time.y)
self.draw()
class FuncTime:
def __init__(self):
self.reset()
def reset(self):
self.x, self.y = [0], [1]
def calculate(self):
return self.y[-1] + self.y[-1] * random.uniform(-0.1, 0.1)
def appends(self, y):
print(self.x[-1], ", ", self.y[-1])
self.x.append(self.x[-1] + 1)
self.y.append(y)
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())

How to make a constant and smooth animation?

So I want to make an animation as the arrow from the Waze app, I want that it moves smoothly and constant. I don't know how to do it inside the QtGraphicsView. My object(arrow) moves by an QVariantAnimation, Which interpolates from the current position until the next position. End it slightly stops. I don't wanna this feature (stops), I want that my animation runs continuous and smoothly. Does anyone know how?
Here is a minimum reproducible example:
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui
import sys
import random
random.seed(0)
class Example(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.button = QtWidgets.QPushButton("Start", self)
self.button.clicked.connect(self.doAnim)
self.button.move(10, 10)
self.scene = QtWidgets.QGraphicsScene()
self.view = QtWidgets.QGraphicsView(self)
self.view.setScene(self.scene)
self.view.setGeometry(150, 30, 500, 800)
self.setGeometry(300, 300, 380, 300)
self.setWindowTitle('Animation')
pen = QtGui.QPen()
pen.setBrush(QtGui.QBrush(QtCore.Qt.darkBlue))
pen.setWidth(5)
self.scene.addEllipse(0,0,10,10, pen)
self.show()
self.ellipse = self.scene.items()[0]
def doAnim(self):
# Every time that I click on the start buttom this animation runs
# and stops,
pos = self.ellipse.pos()
new_pos = QtCore.QPointF(pos.x()+ random.randint(-10, 10), pos.y() +random.randint(-10, 10))
self.anim = QtCore.QVariantAnimation()
self.anim.setDuration(1000)
self.anim.setStartValue(pos)
self.anim.setEndValue(new_pos)
self.anim.setLoopCount(-1)
self.anim.valueChanged.connect(self.ellipse.setPos)
self.anim.start(QtCore.QVariantAnimation.DeleteWhenStopped)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ex = Example()
ex.show()
app.exec_()
The simple solution is to connect to the finished signal of the animation, and set again the start/end values if a new target position is available.
from random import randrange
from PyQt5 import QtCore, QtGui, QtWidgets
class Example(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
layout = QtWidgets.QGridLayout(self)
self.startButton = QtWidgets.QPushButton("Start")
layout.addWidget(self.startButton)
self.addPointButton = QtWidgets.QPushButton("Add target point")
layout.addWidget(self.addPointButton, 0, 1)
self.view = QtWidgets.QGraphicsView()
layout.addWidget(self.view, 1, 0, 1, 2)
self.scene = QtWidgets.QGraphicsScene()
self.view.setScene(self.scene)
self.scene.setSceneRect(-10, -10, 640, 480)
pen = QtGui.QPen(QtCore.Qt.darkBlue, 5)
self.ellipse = self.scene.addEllipse(0, 0, 10, 10, pen)
self.queue = []
self.startButton.clicked.connect(self.begin)
self.addPointButton.clicked.connect(self.addPoint)
self.anim = QtCore.QVariantAnimation()
self.anim.setDuration(1000)
self.anim.valueChanged.connect(self.ellipse.setPos)
self.anim.setStartValue(self.ellipse.pos())
self.anim.finished.connect(self.checkPoint)
def begin(self):
self.startButton.setEnabled(False)
self.addPoint()
def addPoint(self):
self.queue.append(QtCore.QPointF(randrange(600), randrange(400)))
self.checkPoint()
def checkPoint(self):
if not self.anim.state() and self.queue:
if self.anim.currentValue():
# a valid currentValue is only returned when the animation has
# been started at least once
self.anim.setStartValue(self.anim.currentValue())
self.anim.setEndValue(self.queue.pop(0))
self.anim.start()
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui
import sys
import random
import queue
import time
class Position(queue.Queue):
def __init__(self, *args, **kwargs):
super(Position, self).__init__(*args, **kwargs)
class Worker(QtCore.QThread):
pos_signal = QtCore.pyqtSignal(QtCore.QPointF)
def __init__(self, rect, parent=None, *args, **kwargs):
super().__init__(parent)
random.seed(0)
self.pos = QtCore.QPointF(0, 0)
self.rect: QtCore.QRectF = rect
def run(self) -> None:
new_pos = QtCore.QPointF(self.pos.x()+ random.randint(-100, 100), self.pos.y() +random.randint(-100, 100))
if not self.rect.contains(new_pos):
self.run()
else:
self.pos = new_pos
self.pos_signal.emit(new_pos)
time.sleep(0.1)
self.run()
class Example(QtWidgets.QWidget):
next_signal = QtCore.pyqtSignal()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.button = QtWidgets.QPushButton("Start", self)
self.button.clicked.connect(self.doAnim)
self.scene = QtWidgets.QGraphicsScene()
self.view = QtWidgets.QGraphicsView(self)
self.view.setScene(self.scene)
self.view.setGeometry(100, 100, 500, 800)
self.setGeometry(300, 300, 800, 800)
pen = QtGui.QPen()
pen.setBrush(QtGui.QBrush(QtCore.Qt.darkBlue))
pen.setWidth(5)
self.scene.addEllipse(0,0,20,10, pen)
self.ellipse = self.scene.items()[0]
self.rect = QtCore.QRectF(0,0, 200, 200)
self.scene.addRect(self.rect, pen)
self.positions = Position()
self.producer = Worker(rect=self.rect)
self.producer.pos_signal.connect(self.positions.put)
self.producer.start()
self.old = QtCore.QPointF(0, 0)
self.new = None
def ready(self):
self.next_signal.emit()
self.old = self.new
self.doAnim()
def doAnim(self):
# Every time that I click on the start buttom this animation runs
# and stops,
self.sequential_animation = QtCore.QSequentialAnimationGroup(self)
self.new = self.positions.get()
self.anim = QtCore.QVariantAnimation()
self.anim.setDuration(1000)
self.anim.setStartValue(self.old)
self.anim.setEndValue(self.new)
self.anim.valueChanged.connect(self.ellipse.setPos)
self.anim.finished.connect(self.ready)
self.sequential_animation.addAnimation(self.anim)
self.sequential_animation.start(QtCore.QSequentialAnimationGroup.DeleteWhenStopped)
# self.anim.start(QtCore.QVariantAnimation.DeleteWhenStopped)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ex = Example()
ex.show()
app.exec_()

Add/Delete plots independently on a Matplotlib figure

I want to generate a scatterplot (up to half a million points) and on top of that, add different statistics (e.g. Q1, median, Q3). The idea is to add/delete those statistics without replotting the scatterplot in order to speed up the process. So far I can add plots independently on the figure but I can't delete a specific plot.
When I uncheck the checkbox, I get the following error:
AttributeError: 'Graphics' object has no attribute 'vline1'
I understand that when I create the plot, I need to store/return the plot in order to call it later when I want to delete it but I don't know how to do that.
Here my current code:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.pyplot import Figure
class Mainwindow(QMainWindow):
def __init__(self, parent=None):
super(Mainwindow, self).__init__(parent)
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
self.fig = Figure()
self.axes = self.fig.add_subplot(111)
self.canvas = FigureCanvas(self.fig)
self.gridLayout = QGridLayout(centralWidget)
self.gridLayout.addWidget(self.canvas)
self.btn_plot = QCheckBox("Plot")
self.btn_line = QCheckBox("Line")
self.gridLayout.addWidget(self.btn_plot, 1,0,1,1)
self.gridLayout.addWidget(self.btn_line, 2,0,1,1)
self.btn_plot.clicked.connect(self.btnPlot)
self.btn_line.clicked.connect(self.btnLine)
def btnPlot(self):
self.checked = self.btn_plot.isChecked()
self.Graphics = Graphics('plot', self.checked, self.axes)
def btnLine(self):
self.checked = self.btn_line.isChecked()
self.Graphics = Graphics('line', self.checked, self.axes)
class Graphics:
def __init__(self, typeGraph, checked, axes):
self.typeGraph = typeGraph
self.checked = checked
self.axes = axes
if self.typeGraph == 'plot': self.drawPlot()
if self.typeGraph == 'line': self.drawLine()
def drawPlot(self):
if self.checked == True:
self.plot = self.axes.plot([10,20,30], [5,10,2], 'o')
else:
self.plot.remove()
self.axes.figure.canvas.draw()
def drawLine(self):
if self.checked == True:
self.vline1 = self.axes.axvline(x=15, linestyle="dashed", color="#595959")
self.vline2 = self.axes.axvline(x=25, linestyle="dashed", color="#595959")
else:
self.vline1.remove()
self.vline2.remove()
self.axes.figure.canvas.draw()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
prog = Mainwindow()
prog.show()
sys.exit(app.exec_())
Problem is be because when you click it then it creates always new Graphics (in btnPlot/btnLine) which doesn't have previous values - plot, vline1, vline2. You have to create Graphics only once and later run only drawPlot(checked), drawLine(checked) to add or remove item.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.pyplot import Figure
class Mainwindow(QMainWindow):
def __init__(self, parent=None):
super(Mainwindow, self).__init__(parent)
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
self.fig = Figure()
self.axes = self.fig.add_subplot(111)
self.canvas = FigureCanvas(self.fig)
self.gridLayout = QGridLayout(centralWidget)
self.gridLayout.addWidget(self.canvas)
self.btn_plot = QCheckBox("Plot")
self.btn_line = QCheckBox("Line")
self.gridLayout.addWidget(self.btn_plot, 1,0,1,1)
self.gridLayout.addWidget(self.btn_line, 2,0,1,1)
self.btn_plot.clicked.connect(self.btnPlot)
self.btn_line.clicked.connect(self.btnLine)
# create only once
self.Graphics = Graphics(self.axes)
def btnPlot(self):
# add or remove
self.Graphics.drawPlot(self.btn_plot.isChecked())
def btnLine(self):
# add or remove
self.Graphics.drawLine(self.btn_line.isChecked())
class Graphics:
def __init__(self, axes):
self.axes = axes
# create at start with default values (but frankly, now I don't need it)
self.plot = None
self.vline1 = None
self.vline2 = None
def drawPlot(self, checked):
if checked:
self.plot = self.axes.plot([10,20,30], [5,10,2], 'o')
else:
for item in self.plot:
item.remove()
self.axes.figure.canvas.draw()
def drawLine(self, checked):
if checked:
self.vline1 = self.axes.axvline(x=15, linestyle="dashed", color="#595959")
self.vline2 = self.axes.axvline(x=25, linestyle="dashed", color="#595959")
else:
self.vline1.remove()
self.vline2.remove()
self.axes.figure.canvas.draw()
if __name__ == "__main__":
app = QtWidgets.QApplication([])
prog = Mainwindow()
prog.show()
sys.exit(app.exec())

Live plotting of many subplots using pyqtgraph

I used this question and tried to use it to do fast live plotting of many subplots.
Unfortunately it is very difficult for me to understand the code and thus I have problems changing it for my needs.
I wanted to create a subplot of 2x2 matrices with 10x10 pixels. Right now I am getting the following:
The code looks like that:
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
import numpy as np
import time
import sys
class App(QtGui.QMainWindow):
def __init__(self, parent=None):
super(App, self).__init__(parent)
#### Create Gui Elements ###########
self.mainbox = QtGui.QWidget()
self.setCentralWidget(self.mainbox)
self.mainbox.setLayout(QtGui.QVBoxLayout())
self.canvas = pg.GraphicsLayoutWidget()
self.mainbox.layout().addWidget(self.canvas)
self.label = QtGui.QLabel()
self.mainbox.layout().addWidget(self.label)
self.view = self.canvas.addViewBox()
self.view.setAspectLocked(True)
self.view.setRange(QtCore.QRectF(0, 0, 50, 50))
self.img = []
for i in range(4):
self.img.append(pg.ImageItem(None, border="w"))
self.canvas.nextRow()
self.view.addItem(self.img[i])
self._update()
def _update(self):
for i in range(4):
self.data = np.random.rand(10,10)
self.img[i].setImage(self.data)
QtCore.QTimer.singleShot(1, self._update)
def sensor_data(n_sensors, x_res, y_res):
return np.random.rand(n_sensors, x_res, y_res)
if __name__ == '__main__':
while True:
# Get sensor data
data = sensor_data(4, 10, 10)
# Pass data to live plot function?
app = QtGui.QApplication(sys.argv)
thisapp = App()
thisapp.show()
sys.exit(app.exec_())
Can someone please show me what I'm doing wrong?
You have only built a single ViewBox and in it you are adding the items and that causes the problem, what you must do is create several ViewBox and add an item as I show you next:
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
import numpy as np
class App(QtGui.QMainWindow):
def __init__(self, parent=None):
super(App, self).__init__(parent)
self.mainbox = QtGui.QWidget()
self.setCentralWidget(self.mainbox)
self.canvas = pg.GraphicsLayoutWidget()
self.label = QtGui.QLabel()
lay = QtGui.QVBoxLayout(self.mainbox)
lay.addWidget(self.canvas)
lay.addWidget(self.label)
self.img_items = []
for i in range(4):
view = self.canvas.addViewBox()
view.setAspectLocked(True)
view.setRange(QtCore.QRectF(0, 0, 10, 10))
it = pg.ImageItem(None, border="w")
view.addItem(it)
self.img_items.append(it)
self.canvas.nextRow()
timer = QtCore.QTimer(self, interval=1)
timer.timeout.connect(self._update)
timer.start()
def _update(self):
for item in self.img_items:
data = np.random.rand(10, 10)
item.setImage(data)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
thisapp = App()
thisapp.show()
sys.exit(app.exec_())
Update:
NxN
n = 2
for i in range(n):
for j in range(n):
view = self.canvas.addViewBox(i, j)
view.setAspectLocked(True)
view.setRange(QtCore.QRectF(0, 0, 10, 10))
it = pg.ImageItem(None, border="w")
view.addItem(it)
self.img_items.append(it)
Update:
If you want to obtain data within a while True you must do it in a new thread to avoid the GUI being blocked, you must also give a small sleep so that the GUI can be updated:
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
import numpy as np
def sensor_data(n_sensors, x_res, y_res):
return np.random.rand(n_sensors, x_res, y_res)
class Thread(QtCore.QThread):
dataChanged = QtCore.pyqtSignal(np.ndarray)
def run(self):
while True:
data = sensor_data(4, 10, 10)
self.dataChanged.emit(data)
QtCore.QThread.msleep(10)
class App(QtGui.QMainWindow):
def __init__(self, parent=None):
super(App, self).__init__(parent)
self.mainbox = QtGui.QWidget()
self.setCentralWidget(self.mainbox)
self.canvas = pg.GraphicsLayoutWidget()
self.label = QtGui.QLabel()
lay = QtGui.QVBoxLayout(self.mainbox)
lay.addWidget(self.canvas)
lay.addWidget(self.label)
self.img_items = []
n = 2
for i in range(n):
for j in range(n):
view = self.canvas.addViewBox(i, j)
view.setAspectLocked(True)
view.setRange(QtCore.QRectF(0, 0, 10, 10))
it = pg.ImageItem(None, border="w")
view.addItem(it)
self.img_items.append(it)
#QtCore.pyqtSlot(np.ndarray)
def update_data(self, data):
for i, v in enumerate(data):
self.img_items[i].setImage(v)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
thisapp = App()
thread = Thread()
thread.dataChanged.connect(thisapp.update_data)
thread.start()
thisapp.show()
sys.exit(app.exec_())

draw matplotlib graph with QThread issue

Beside main class Window, I have defined new class (test_graph_Data) which will draw graph with matplotlib. In order to avoid problem with alarm "QPixmap: It is not safe to use pixmaps outside the GUI thread", I have defined new class test_graph_Data, which will emit signal to main class for drawing the graph, but there are some problems to emit the signals... Look at the code below:
When I run the code, I got the warning: AttributeError: "PyQt4.QtCore.pyqtSignal" object has no attribute 'connect'!
import sys, time
from PyQt4 import QtGui, QtCore
import matplotlib.pyplot as plt
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50, 50, 120, 90)
self.home()
def home(self):
self.test = QtGui.QPushButton("Test", self)
self.test.clicked.connect(self.test1_function)
self.test.move(10,20)
self.show()
def test1_function(self):
self.get_thread = test_graph_Data()
self.connect(self.get_thread, QtCore.SIGNAL("finished()"),self.done_test1_function)
self.get_thread.start()
def done_test1_function(self):
print 'Graph is displayed!'
class test_graph_Data(QtCore.QThread) :
def __init__(self):
QtCore.QThread.__init__(self)
def __del__(self):
self.wait()
def graph_data(self):
start = time.time()
b = [1,0,1,0,1,0,1,1,1,1,0,1,0,1]
plt.ion()
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
ax1.plot(b, 'b')
end = time.time()
print end - start
def run(self):
top_post = self.graph_data()
def main():
app = QtGui.QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())
if __name__ == '__main__' :
main()
You must use the thread to manipulate the data, not to graph. In this example I have made your list rotate. You must create a signal that communicates to the main thread that the data is ready to be updated. In the GUI you must connect that signal with a function that updates the graph.
import sys
import time
import matplotlib as mpl
mpl.use("QT4Agg")
import matplotlib.pyplot as plt
from PyQt4 import QtGui, QtCore
class test_graph_Data(QtCore.QThread):
updated = QtCore.pyqtSignal(list)
running = True
def __init__(self, parent=None):
QtCore.QThread.__init__(self, parent)
self.b = [1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1]
def run(self):
while self.running:
# rotate list
self.b = self.b[1:] + [self.b[0]]
self.updated.emit(self.b)
time.sleep(0.1)
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50, 50, 120, 90)
self.home()
def home(self):
self.test = QtGui.QPushButton("Test", self)
self.test.clicked.connect(self.test1_function)
self.test.move(10, 20)
self.show()
def test1_function(self):
self.get_thread = test_graph_Data(self)
self.get_thread.finished.connect(self.done_test1_function)
self.get_thread.updated.connect(self.graph_data)
plt.ion()
fig1 = plt.figure()
self.ax1 = fig1.add_subplot(111)
self.get_thread.start()
def done_test1_function(self):
print('Graph is displayed!')
def graph_data(self, data):
self.ax1.clear()
self.ax1.plot(data, 'b')
def closeEvent(self, event):
self.get_thread.running = False
self.get_thread.wait()
def main():
app = QtGui.QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

Categories

Resources