Show Matplotlib Imshow Output In Qt
Solution 1:
From what I read the OP has an XY problem, that is, its objective is to show the output of imshow() in a Qt window, but ask about the attempt to display the data in a QImage.
The imshow() method does not show raw data but processes the information based on the parameters as indicated by the docs:
matplotlib.pyplot.imshow(X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, shape=, filternorm=1, filterrad=4.0, imlim=, resample=None, url=None, *, data=None, **kwargs)
So if you want to obtain an image with that data you must implement that algorithm (you can check the source code of matplotlib or similar SW to analyze the logic)
If we focus on the real objective then the simplest solution is to use the Qt backend of matplotlib to obtain the appropriate canvas as shown below:
import numpy as np
from PyQt5 import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
classMainWindow(QtWidgets.QMainWindow):
def__init__(self, parent=None):
super().__init__(parent)
self.figure = Figure(figsize=(5, 3))
self.canvas = FigureCanvas(self.figure)
self.ax = self.figure.subplots()
delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-(X ** 2) - Y ** 2)
Z2 = np.exp(-((X - 1) ** 2) - (Y - 1) ** 2)
Z = (Z1 - Z2) * 2
self.ax.imshow(Z)
self.ax.set_axis_off()
self.setCentralWidget(self.canvas)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
Update:
If you want to display the data from time to time then you can use a QTimer that updates the information as I show below:
import random
import numpy as np
from PyQt5 import QtCore, QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.figure = Figure(figsize=(5, 3))
self.canvas = FigureCanvas(self.figure)
self.ax = self.figure.subplots()
self.ax.set_axis_off()
self.setCentralWidget(self.canvas)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.on_timeout)
timer.start(100)
def on_timeout(self):
x0, y0 = random.uniform(-2, 2), random.uniform(-2, 2)
delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-(X ** 2) - Y ** 2)
Z2 = np.exp(-((X - x0) ** 2) - (Y - y0) ** 2)
Z = (Z1 - Z2) * 2
self.ax.imshow(Z)
self.canvas.draw()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
On the other hand, if you want to have a SW in real time then the GUI will limit that objective. It is advisable to show the data every N samples so that the GUI is not blocked and the user can view and analyze the information. The human eye is very slow, so even if the technology exists to display images every microsecond, our vision would not appreciate it, our vision requires 60ms to process the image, therefore the devices are designed to work at 30Hz since if the frequency were superior improvement would not be observed.
Post a Comment for "Show Matplotlib Imshow Output In Qt"