PyQt5 Pyqtgraph plot discret - python

At present, for this project, I'm working with matplolib to trace my plot.
Nevertheless, I wish to convert at pyqtgraph ( Pyqt5).
I didn't found how trace a discret plot of FFT with pyqtgraph, same the picture.
Have you an idea ?

You can create an item in which the vertical lines are drawn:
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
class LinePlot(pg.GraphicsObject):
def __init__(self, data):
super(LinePlot, self).__init__()
self._data = []
self.setData(data)
def setData(self, data):
self._data = data
self.generate()
def generate(self):
self.picture = QtGui.QPicture()
p = QtGui.QPainter(self.picture)
p.setPen(pg.mkPen('r', width=2))
for (t, v) in self._data:
if v != 0:
p.drawLine(QtCore.QPointF(t, 0), QtCore.QPointF(t, v))
def paint(self, p, *args):
p.drawPicture(0, 0, self.picture)
def boundingRect(self):
return QtCore.QRectF(self.picture.boundingRect())
if __name__ == '__main__':
import sys
import random
app = QtGui.QApplication(sys.argv)
w = QtGui.QMainWindow()
view = pg.GraphicsLayoutWidget()
w.setCentralWidget(view)
plot = view.addPlot()
vals = [(i, random.randint(0, 255)) for i in range(30)]
plot.addItem(LinePlot(vals))
w.show()
sys.exit(app.exec_())

Related

Matching the scatter points colour with the Image colour bar

I have made this GUI with PyQt5:
How can I make the colour of the points in the bottom plot match the colour bar in the 2D plot above? And change accordingly when I change the bar max/min ?
MWE:
from PyQt5 import QtGui, QtCore
import pyqtgraph as pg
import sys
import numpy as np
width = 1000
height = 500
x = np.linspace(-10,10,100)
def func():
X, Y = np.meshgrid(x, x)
return np.exp(-X**2/10 - Y**2/2)
array = func()
sumxaxis = np.sum(array, axis=0)
sumyaxis = np.sum(array, axis=1)
class layout():
def setup(self, window):
self.window = window
self.window.resize(width, height)
self.centralwidget = QtGui.QWidget(self.window)
self.horizontallayout = QtGui.QHBoxLayout(self.centralwidget)
self.window.setCentralWidget(self.centralwidget)
self.plot = pg.GraphicsLayoutWidget(self.window)
self.horizontallayout.addWidget(self.plot)
self.view = self.plot.addPlot()
self.img = pg.ImageItem(border='k')
self.img.setImage(array, border = 'k')
self.view.addItem(self.img)
self.viewbox = self.view.getViewBox()
self.hist = pg.HistogramLUTItem()
self.hist.setImageItem(self.img)
self.hist.setLevels(0, 1)
self.hist.gradient.loadPreset('viridis')
self.plot.addItem(self.hist, colspan=1)
self.plot.nextRow()
self.plot2 = self.plot.addPlot(colspan=1)
self.plot2.setMaximumHeight(200)
self.plot2.plot(-x,sumyaxis,symbol='o',symbolSize=1, symbolBrush=('w'), pen=None, clear=True)
class Window(pg.Qt.QtGui.QMainWindow, layout):
def __init__(self, shot = None):
super(Window, self).__init__()
self.setup(self)
self.show()
if __name__ == '__main__':
app = pg.Qt.QtGui.QApplication([])
Window()
sys.exit(app.exec_())
UPDATE
I managed to change the scatter plot to the same colourbar as the 2D image, but I have not yet managed to make it change its colour as I drag the colour bar cursor!
New MWE:
from PyQt5 import QtGui, QtCore
import pyqtgraph as pg
import sys
import numpy as np
width = 1000
height = 500
x = np.linspace(-10,10,100)
def func():
X, Y = np.meshgrid(x, x)
return np.exp(-X**2/10 - Y**2/2)
array = func()
sumyaxis = array[:, len(array)//2]
class layout():
def setup(self, window):
self.window = window
self.window.resize(width, height)
self.centralwidget = QtGui.QWidget(self.window)
self.horizontallayout = QtGui.QHBoxLayout(self.centralwidget)
self.window.setCentralWidget(self.centralwidget)
self.plot = pg.GraphicsLayoutWidget(self.window)
self.horizontallayout.addWidget(self.plot)
self.view = self.plot.addPlot()
self.img = pg.ImageItem(border='k')
self.img.setImage(array, border = 'k')
self.view.addItem(self.img)
self.viewbox = self.view.getViewBox()
self.hist = pg.HistogramLUTItem()
self.hist.setImageItem(self.img)
self.hist.setLevels(0, 1)
self.hist.gradient.loadPreset('viridis')
self.plot.addItem(self.hist, colspan=1)
self.colours = self.hist.getLookupTable(img=array)
self.cmap = pg.ColorMap(pos=np.linspace(self.hist.getLevels()[0], self.hist.getLevels()[1], len(self.colours)), color=self.colours)
self.plot.nextRow()
self.plot2 = self.plot.addPlot(colspan=1)
self.plot2.setMaximumHeight(200)
self.c = self.cmap.map(sumyaxis, 'qcolor')
self.plot2.plot(-x,sumyaxis,symbol='o',symbolSize=10, symbolBrush = self.c, pen=None, clear=True)
class Window(pg.Qt.QtGui.QMainWindow, layout):
def __init__(self, shot = None):
super(Window, self).__init__()
self.setup(self)
self.show()
if __name__ == '__main__':
app = pg.Qt.QtGui.QApplication([])
w = Window()
sys.exit(app.exec_())
You have to use the sigLookupTableChanged signal and implement the logic to update the symbolBrush of PlotDataItem:
# ...
self.plot.nextRow()
self.plot2 = self.plot.addPlot(colspan=1)
self.plot2.setMaximumHeight(200)
self.curve = self.plot2.plot(
-x,
sumyaxis,
symbol="o",
symbolSize=10,
pen=None,
clear=True,
)
self.hist.sigLookupTableChanged.connect(self.handle_sigLookupTableChanged)
self.handle_sigLevelsChanged()
def handle_sigLookupTableChanged(self):
colours = self.hist.getLookupTable(img=array)
cmap = pg.ColorMap(
pos=np.linspace(*self.hist.getLevels(), len(colours)),
color=colours,
)
c = cmap.map(sumyaxis, "qcolor")
self.curve.opts["symbolBrush"] = c
self.curve.updateItems()

How to create a basic custom QGraphicsEffect in Qt?

I have been trying to create a basic QGraphicsEffect to change the colors of the widget, but first I tried to make an effect that does nothing like so:
class QGraphicsSepiaEffect(QtWidgets.QGraphicsEffect):
def draw(painter):
pixmap = sourcePixmap()
painter.drawPixmap(pixmap.rect(), pixmap)
I am using PySide2. Though I checked all over the internet but couldn't find any sample, neither a template nor a real custom effect.
How can I write a basic effect to alter the colors of my widget?
As your question is basically how to create a custom effect then based on an example offered by the Qt community I have translated it to PySide2:
import random
import sys
from PySide2 import QtCore, QtGui, QtWidgets
# or
# from PyQt5 import QtCore, QtGui, QtWidgets
class HighlightEffect(QtWidgets.QGraphicsEffect):
def __init__(self, offset=1.5, parent=None):
super(HighlightEffect, self).__init__(parent)
self._color = QtGui.QColor(255, 255, 0, 128)
self._offset = offset * QtCore.QPointF(1, 1)
#property
def offset(self):
return self._offset
#property
def color(self):
return self._color
#color.setter
def color(self, color):
self._color = color
def boundingRectFor(self, sourceRect):
return sourceRect.adjusted(
-self.offset.x(), -self.offset.y(), self.offset.x(), self.offset.y()
)
def draw(self, painter):
offset = QtCore.QPoint()
try:
pixmap = self.sourcePixmap(QtCore.Qt.LogicalCoordinates, offset)
except TypeError:
pixmap, offset = self.sourcePixmap(QtCore.Qt.LogicalCoordinates)
bound = self.boundingRectFor(QtCore.QRectF(pixmap.rect()))
painter.save()
painter.setPen(QtCore.Qt.NoPen)
painter.setBrush(self.color)
p = QtCore.QPointF(offset.x() - self.offset.x(), offset.y() - self.offset.y())
bound.moveTopLeft(p)
painter.drawRoundedRect(bound, 5, 5, QtCore.Qt.RelativeSize)
painter.drawPixmap(offset, pixmap)
painter.restore()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QWidget()
lay = QtWidgets.QVBoxLayout(w)
for _ in range(3):
o = QtWidgets.QLabel()
o.setStyleSheet(
"""background-color : {}""".format(
QtGui.QColor(*random.sample(range(255), 3)).name()
)
)
effect = HighlightEffect(parent=o)
o.setGraphicsEffect(effect)
lay.addWidget(o)
w.show()
w.resize(640, 480)
sys.exit(app.exec_())

How to plot data from QTableWidget into QChart

I'm working on data analysis software, which takes data from remote database and puts it into QTableWidget. How could I effectively get these data from table and put them into QChart?
I've seen that if I had been using QTableView, it could have been done with models, but as I understand it, using QTableView would be far more complicated for my scenario.
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtCore import *
from PySide2.QtCharts import *
import sys
import random
class DateTimeDelegate(QStyledItemDelegate):
def initStyleOption(self, option, index):
super(DateTimeDelegate, self).initStyleOption(option, index)
value = index.data()
option.text =
QDateTime.fromMSecsSinceEpoch(value).toString("dd.MM.yyyy")
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setGeometry(0, 0, 1280, 400)
self.chart_table()
self.populate()
def chart_table(self):
self.table = QTableWidget(0, 2)
delegate = DateTimeDelegate(self.table)
self.table.setItemDelegateForColumn(0, delegate)
chart = QtCharts.QChart()
self.chartView = QtCharts.QChartView(chart)
self.chartView.setFixedSize(600, 430)
splitter = QSplitter(self)
splitter.addWidget(self.table)
splitter.addWidget(self.chartView)
self.setCentralWidget(splitter)
series = QtCharts.QLineSeries(name='Odoslané')
mapper = QtCharts.QVXYModelMapper(xColumn=0, yColumn=2)
mapper.setModel(self.table.model())
mapper.setSeries(series)
chart.addSeries(mapper.series())
self.axis_X = QtCharts.QDateTimeAxis()
self.axis_X.setFormat("MMM yyyy")
self.axis_Y = QtCharts.QValueAxis()
chart.setAxisX(self.axis_X, series)
chart.setAxisY(self.axis_Y, series)
self.axis_Y.setRange(0, 0)
self.axis_Y.setLabelFormat('%.0f')
self.axis_X.setRange(QDate(2017, 10, 1), QDate.currentDate())
chart.setTitle('Chart')
def addRow(self, dt, value):
self.table.insertRow(0)
for col, v in enumerate((dt.toMSecsSinceEpoch(), value)):
it = QTableWidgetItem()
it.setData(Qt.DisplayRole, dt.toMSecsSinceEpoch())
self.table.setItem(0, 0, it)
t_m, t_M = self.axis_X.min(), self.axis_X.max()
t_m = min(t_m, dt)
t_M = max(t_M, dt)
m, M = self.axis_Y.min(), self.axis_Y.max()
m = min(m, value)
M = max(M, value)
In this method I simulate filling table with data as I get them from database.
def populate(self):
for i in range(4):
count=random.randint(1,40)
value_str = QDate.currentDate().addDays(count).toString('dd.MM.yyyy')
dt = QDateTime.fromString(value_str, "dd.MM.yyyy")
sent = QTableWidgetItem(str(count))
value = int(sent.text())
self.addRow(dt, value)
self.table.setItem(0, 1, sent)
And App running function -
def main():
app = QApplication(sys.argv)
gui = MainWindow()
gui.show()
sys.exit(app.exec_())
main()
The easiest way to show the data of a QTableWidget in a QChartView is to use a QVXYModelMapper that relates the model of the QTableWidget with a QLineSerie. But for this the data stored in the QTableWidget should not be a string but an integer so you should not convert the QDateTime to string using toString(), but to an integer using toMSecsSinceEpoch(), and to show it as datetime in the QTableWidget a delegate should be used.
In the following example the addRow method allows to add a (QDateTime, value) to a row, this recalculates the ranges of each axis.
import random
from functools import partial
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtCharts import QtCharts
class DateTimeDelegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(DateTimeDelegate, self).initStyleOption(option, index)
value = index.data()
option.text = QtCore.QDateTime.fromMSecsSinceEpoch(value).toString("dd.MM.yyyy")
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.m_tablewidget = QtWidgets.QTableWidget(0, 2)
delegate = DateTimeDelegate(self.m_tablewidget)
self.m_tablewidget.setItemDelegateForColumn(0, delegate)
self.m_chartview = QtCharts.QChartView()
self.m_chartview.chart().setTheme(QtCharts.QChart.ChartThemeQt)
self.m_chartview.setMinimumWidth(400)
self.m_series = QtCharts.QLineSeries(name="Time-Value")
self.m_mapper = QtCharts.QVXYModelMapper(self, xColumn=0, yColumn=1)
self.m_mapper.setModel(self.m_tablewidget.model())
self.m_mapper.setSeries(self.m_series)
self.m_chartview.chart().addSeries(self.m_mapper.series())
splitter = QtWidgets.QSplitter(self)
splitter.addWidget(self.m_tablewidget)
splitter.addWidget(self.m_chartview)
self.setCentralWidget(splitter)
self.m_time_axis = QtCharts.QDateTimeAxis()
self.m_time_axis.setFormat("dd.MM.yyyy")
self.m_value_axis = QtCharts.QValueAxis()
self.m_chartview.chart().setAxisX(self.m_time_axis, self.m_series)
self.m_chartview.chart().setAxisY(self.m_value_axis, self.m_series)
self.m_value_axis.setRange(0, 0)
self.m_time_axis.setRange(
QtCore.QDateTime.currentDateTime(),
QtCore.QDateTime.currentDateTime().addDays(1),
)
def addRow(self, dt, value):
row = self.m_tablewidget.rowCount()
self.m_tablewidget.insertRow(row)
for col, v in enumerate((dt.toMSecsSinceEpoch(), value)):
it = QtWidgets.QTableWidgetItem()
it.setData(QtCore.Qt.DisplayRole, v)
self.m_tablewidget.setItem(row, col, it)
t_m, t_M = self.m_time_axis.min(), self.m_time_axis.max()
t_m = min(t_m, dt)
t_M = max(t_M, dt)
m, M = self.m_value_axis.min(), self.m_value_axis.max()
m = min(m, value)
M = max(M, value)
self.m_time_axis.setRange(t_m, t_M)
self.m_value_axis.setRange(m, M)
counter = 0
def onTimeout(w):
# Emulate the data
global counter
dt = QtCore.QDateTime.currentDateTime().addDays(counter)
value = random.uniform(-100, 100)
w.addRow(dt, value)
counter += 1
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
wrapper = partial(onTimeout, w)
timer = QtCore.QTimer(timeout=wrapper, interval=1000)
timer.start()
sys.exit(app.exec_())
Update:
You do not have to create any QTableWidget in the populate method. I have corrected your logic so that it is added to the top of the QTableWidget, also I have corrected the calculation of the range.
import sys
import random
from PySide2.QtCore import *
from PySide2.QtWidgets import *
from PySide2.QtCharts import QtCharts
class DateTimeDelegate(QStyledItemDelegate):
def initStyleOption(self, option, index):
super(DateTimeDelegate, self).initStyleOption(option, index)
value = index.data()
option.text = QDateTime.fromMSecsSinceEpoch(value).toString("dd.MM.yyyy")
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setGeometry(0, 0, 1280, 400)
self.chart_table()
self.populate()
def chart_table(self):
self.table = QTableWidget(0, 2)
delegate = DateTimeDelegate(self.table)
self.table.setItemDelegateForColumn(0, delegate)
chart = QtCharts.QChart()
self.chartView = QtCharts.QChartView(chart)
self.chartView.setFixedSize(600, 430)
splitter = QSplitter(self)
splitter.addWidget(self.table)
splitter.addWidget(self.chartView)
self.setCentralWidget(splitter)
series = QtCharts.QLineSeries(name="Odoslané")
mapper = QtCharts.QVXYModelMapper(self, xColumn=0, yColumn=1)
mapper.setModel(self.table.model())
mapper.setSeries(series)
chart.addSeries(mapper.series())
self.axis_X = QtCharts.QDateTimeAxis()
self.axis_X.setFormat("MMM yyyy")
self.axis_Y = QtCharts.QValueAxis()
chart.setAxisX(self.axis_X, series)
chart.setAxisY(self.axis_Y, series)
self.axis_Y.setRange(0, 0)
self.axis_Y.setLabelFormat("%.0f")
chart.setTitle("Chart")
def addRow(self, dt, value):
self.table.insertRow(0)
for col, v in enumerate((dt.toMSecsSinceEpoch(), value)):
it = QTableWidgetItem()
it.setData(Qt.DisplayRole, v)
self.table.setItem(0, col, it)
if self.table.rowCount() == 1:
self.axis_X.setRange(dt, dt.addDays(1))
self.axis_Y.setRange(v, v)
else:
t_m, t_M = self.axis_X.min(), self.axis_X.max()
t_m = min(t_m, dt)
t_M = max(t_M, dt)
m, M = self.axis_Y.min(), self.axis_Y.max()
m = min(m, value)
M = max(M, value)
self.axis_X.setRange(t_m, t_M)
self.axis_Y.setRange(m, M)
def populate(self):
for i in range(100):
# simulate filling table with data as I get them from database.
value = random.uniform(1, 40)
fake_dt_str = QDate.currentDate().addDays(i).toString("dd.MM.yyyy")
fake_value_str = str(random.uniform(0, 2))
# Convert simulated data
dt = QDateTime.fromString(fake_dt_str, "dd.MM.yyyy")
value = float(fake_value_str)
self.addRow(dt, value)
def main():
app = QApplication(sys.argv)
gui = MainWindow()
gui.show()
sys.exit(app.exec_())
main()

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_())

Create several popup plots with FigureCanvas in PyQt4

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

Categories

Resources