How to display images using PyQt5 on a widget? - python

I am making Wafer contour map program by PyQt5. I have some problems.
import pandas as pd
test = pd.DataFrame(index=range(0,101), columns=range(0,101)) # empty dataframe
for i in range(0,101): #
for j in range(0,101):
if ((50-i)**2 + (50-j)**2) < 50**2:
test.loc[i,j] = i*2+i+j
else:
test.loc[i,j] = 0
import numpy as np
import matplotlib as mpl
import matplotlib.pylab as plt
test[test == 0] = np.nan
plt.figure(1)
plt.imshow(test, interpolation = 'hanning', vmin = 0, cmap = 'gist_rainbow_r')
plt.colorbar()
I got a below image
and I would like to display this image instead of graph on PyQt5 window
My window is below like
from PyQt5.uic import loadUiType
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import (
FigureCanvasQTAgg as FigureCanvas,
NavigationToolbar2QT as NavigationToolbar)
Ui_MainWindow, QMainWindow = loadUiType(r'C:\Users\wtjang\.spyder-py3\window.ui')
import numpy as np
from PyQt5 import QtWidgets
class Main(QMainWindow, Ui_MainWindow):
def __init__(self, ):
super(Main, self).__init__()
self.setupUi(self)
def addmpl(self, fig):
self.canvas = FigureCanvas(fig)
self.mplvl.addWidget(self.canvas)
self.canvas.draw()
if __name__ == '__main__':
import sys
from PyQt5 import QtWidgets
fig1 = Figure()
ax1f1 = fig1.add_subplot(111)
ax1f1.plot(np.random.rand(5))
app = QtWidgets.QApplication(sys.argv)
main = Main()
main.addmpl(fig1)
main.show()
sys.exit(app.exec_())
how to change graph to wafer image?
I tried to
if __name__ == '__main__':
import sys
from PyQt5 import QtWidgets
fig1 = plt.imshow(test, interpolation = 'hanning', vmin = 0, cmap = 'gist_rainbow_r')
app = QtWidgets.QApplication(sys.argv)
main = Main()
main.addmpl(fig1)
main.show()
sys.exit(app.exec_())
but I failed.

I got a answer. Thank your helping :)
ax1f1.imshow(test, interpolation = 'hanning', vmin = 0, cmap = 'gist_rainbow_r')

Related

Problem using qt5agg in python matplotlib: Window not displaying anything

I want to display charts using matplotlib's qt5agg backend.
The tutorial's code runs fine:
import sys
import numpy as np
from matplotlib.backends.qt_compat import QtCore, QtWidgets
from matplotlib.backends.backend_qt5agg import (
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
layout = QtWidgets.QVBoxLayout(self._main)
static_canvas = FigureCanvas(Figure(figsize=(5, 3)))
layout.addWidget(static_canvas)
self._static_ax = static_canvas.figure.subplots()
t = np.linspace(0, 10, 501)
self._static_ax.plot(t, np.tan(t), ".")
if __name__ == "__main__":
qapp = QtWidgets.QApplication(sys.argv)
app = ApplicationWindow()
app.show()
qapp.exec_()
It shows a perfect chart.
But the code I've written looks similar, fails to run: no exeption was raised, but no chart showed. Just a blank window.
Here's my code:
import numpy
from matplotlib.pyplot import Figure
from matplotlib.backends.backend_qt5agg import (
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.backends.qt_compat import QtWidgets, QtGui
import sys
class MyWindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.grid = QtWidgets.QGridLayout()
self.canvas = FigureCanvas(Figure(figsize=(3, 2)))
self.grid.addWidget(self.canvas, 0, 0)
self.canvas_fig = self.canvas.figure.subplots()
self.canvas_fig.plot([1, 2, 3, 4, 5], [0., 1., 2., 3., 4.])
if __name__ == "__main__":
qapp = QtWidgets.QApplication()
app = MyWindow()
app.show()
qapp.exec_()
It runs like this:
What's wrong with this code? How to run it successfully?
Can anyone help me?

How do you display a figure in matplotlib using PySide2?

I am trying to learn matplotlib and I tried testing the following code.
import matplotlib
from matplotlib.figure import Figure
import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
import sys
from PySide2 import QtGui
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
wid = QtGui.QWidget()
wid.resize(250, 150)
grid = QtGui.QGridLayout()
wid.setLayout(grid)
fig = Figure(figsize=(7,5), dpi=65, facecolor=(1,1,1), edgecolor=(0,0,0))
canvas = FigureCanvas(fig)
grid.addWidget(canvas)
wid.show()
However it seems like it doesn't recognize the signature of Figure.
TypeError: 'PySide2.QtGui.QGridLayout.addWidget' called with wrong
argument types:
PySide2.QtGui.QGridLayout.addWidget(FigureCanvasQTAgg) Supported
signatures:
PySide2.QtGui.QGridLayout.addWidget(PySide2.QtGui.QWidget, int, int,
PySide2.QtCore.Qt.Alignment = 0)
PySide2.QtGui.QGridLayout.addWidget(PySide2.QtGui.QWidget, int, int,
int, int, PySide2.QtCore.Qt.Alignment = 0)
PySide2 is a binding of Qt5 so you must use backend_qt5agg instead ofbackend_qt4agg, on the other hand it is advisable to import the backend (PySide2 in this case) before matplotlib so that matplotlib configures it internally, in addition to QApplication, QWidget and QGridLayout belong to QtWidgets since Qt5 separates them from QtGui and finally when you add a widget to QGridLayout you must indicate its position in the following parameters.
Considering the above the solution is
import sys
from PySide2 import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
wid = QtWidgets.QWidget()
wid.resize(250, 150)
grid = QtWidgets.QGridLayout(wid)
fig = Figure(figsize=(7, 5), dpi=65, facecolor=(1, 1, 1), edgecolor=(0, 0, 0))
canvas = FigureCanvas(fig)
grid.addWidget(canvas, 0, 0)
wid.show()
sys.exit(app.exec_())

Embedding second animated graph to PyQt5 GUI

I want to add a second animated graph to the GUI holding my first animated graph with both graphs animating at the same time, but I'm not sure how.
Here is my code :
import sys
import numpy as np
from matplotlib.backends.qt_compat import QtWidgets
from matplotlib.backends.backend_qt5agg import (
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure
from matplotlib import animation
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
layout = QtWidgets.QVBoxLayout(self._main)
self.fig = Figure(figsize=(5, 3))
self.canvas = FigureCanvas(self.fig)
layout.addWidget(self.canvas)
self.addToolBar(NavigationToolbar(self.canvas, self))
self.setup()
def setup(self):
self.ax = self.fig.subplots()
self.ax.set_aspect('equal')
self.ax.grid(True, linestyle = '-', color = '0.10')
self.ax.set_xlim([-15, 15])
self.ax.set_ylim([-15, 15])
self.scat = self.ax.scatter([], [], c=(0.9, 0.1, 0.5), zorder=3)
self.scat.set_alpha(0.8)
self.anim = animation.FuncAnimation(self.fig, self.update,
frames = 720, interval = 10)
def update(self, i):
self.scat.set_offsets(([np.cos(np.radians(i))*7.5, np.sin(np.radians(i))*7.5], [0,0]))
if __name__ == "__main__":
qapp = QtWidgets.QApplication(sys.argv)
app = ApplicationWindow()
app.show()
qapp.exec_()
And here is some sample code which has two graphs inside the same window (like how I want to)
Here they use an _update_canvas function for the animated graph and the other graph (which is just a static graph) they plot it in the application window class.
I'm using an update plot function to animate my graph, do I need a second update plot function? How?
Sample code:
import sys
import time
import numpy as np
from matplotlib.backends.qt_compat import QtCore, QtWidgets, is_pyqt5
if is_pyqt5():
from matplotlib.backends.backend_qt5agg import (
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
else:
from matplotlib.backends.backend_qt4agg import (
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
layout = QtWidgets.QVBoxLayout(self._main)
static_canvas = FigureCanvas(Figure(figsize=(5, 3)))
layout.addWidget(static_canvas)
self.addToolBar(NavigationToolbar(static_canvas, self))
dynamic_canvas = FigureCanvas(Figure(figsize=(5, 3)))
layout.addWidget(dynamic_canvas)
self.addToolBar(QtCore.Qt.BottomToolBarArea,
NavigationToolbar(dynamic_canvas, self))
self._static_ax = static_canvas.figure.subplots()
t = np.linspace(0, 10, 501)
self._static_ax.plot(t, np.tan(t), ".")
self._dynamic_ax = dynamic_canvas.figure.subplots()
self._timer = dynamic_canvas.new_timer(
100, [(self._update_canvas, (), {})])
self._timer.start()
def _update_canvas(self):
self._dynamic_ax.clear()
t = np.linspace(0, 10, 101)
# Shift the sinusoid as a function of time.
self._dynamic_ax.plot(t, np.sin(t + time.time()))
self._dynamic_ax.figure.canvas.draw()
if __name__ == "__main__":
qapp = QtWidgets.QApplication(sys.argv)
app = ApplicationWindow()
app.show()
qapp.exec_()
To embed multiple animated graphs, you need to create multiple plot objects(Figure and FigureCanvas) then add each object to the QVBoxLayout. If you wanted to display it horizontally, you can use a QHBoxLayout. Each plot object will have its own subplot, grid, and data. To update each plot's data, you will need each plot to have its individual plot update function where you can pass this to the animation.FuncAnimation handler. So in your case, to have two animated graphs, you will need two update plot functions.
from matplotlib.figure import Figure
from matplotlib import animation
import numpy as np
import sys, matplotlib
from PyQt5 import QtWidgets, QtCore
from matplotlib.backends.backend_qt5agg import (FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
layout = QtWidgets.QVBoxLayout(self._main)
# Configure figure 1
self.fig1 = Figure(figsize=(5, 3))
self.canvas1 = FigureCanvas(self.fig1)
# Configure figure 2
self.fig2 = Figure(figsize=(5, 3))
self.canvas2 = FigureCanvas(self.fig2)
layout.addWidget(self.canvas1)
layout.addWidget(self.canvas2)
self.addToolBar(NavigationToolbar(self.canvas1, self))
self.addToolBar(QtCore.Qt.BottomToolBarArea, NavigationToolbar(self.canvas2, self))
self.setup()
def setup(self):
# Plot 1 (top)
self.ax1 = self.fig1.subplots()
self.ax1.set_aspect('equal')
self.ax1.grid(True, linestyle = '-', color = '0.10')
self.ax1.set_xlim([-15, 15])
self.ax1.set_ylim([-15, 15])
# Plot 2 (bottom)
self.ax2 = self.fig2.subplots()
self.ax2.set_aspect('equal')
self.ax2.grid(True, linestyle = '-', color = '0.10')
self.ax2.set_xlim([-15, 15])
self.ax2.set_ylim([-15, 15])
self.scat1 = self.ax1.scatter([], [], c=(0.9, 0.1, 0.5), zorder=3)
self.scat1.set_alpha(0.8)
self.scat2 = self.ax2.scatter([], [], c=(0.9, 0.1, 0.5), zorder=3)
self.scat2.set_alpha(0.8)
self.anim1 = animation.FuncAnimation(self.fig1, self.update1,frames = 720, interval = 10)
self.anim2 = animation.FuncAnimation(self.fig2, self.update2,frames = 720, interval = 10)
# Update data for plot 1
def update1(self, i):
self.scat1.set_offsets(([np.cos(np.radians(i))*7.5, np.sin(np.radians(i))*7.5], [0,0]))
# Update data for plot 2
def update2(self, i):
self.scat2.set_offsets(([np.cos(np.radians(i))*7.5, np.sin(np.radians(i))*7.5], [0,0]))
if __name__ == "__main__":
qapp = QtWidgets.QApplication(sys.argv)
app = ApplicationWindow()
app.show()
qapp.exec_()
Is there a specific reason you need two separate canvas and two separate figures? If not, then I agree with ImportanceOfBeingErnest's comment and that you should create only one figure/canvas with 2 subplots, and call a single update function that takes care of updating the content of both axes.
In essence, your question would be a duplicate of this one, except for the fact that you are embedding the animation in a Qt app.
import sys
import time
import numpy as np
from matplotlib.backends.qt_compat import QtCore, QtWidgets, is_pyqt5
if is_pyqt5():
from matplotlib.backends.backend_qt5agg import (
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
else:
from matplotlib.backends.backend_qt4agg import (
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
layout = QtWidgets.QVBoxLayout(self._main)
self.fig = Figure(figsize=(5, 6))
self.canvas = FigureCanvas(self.fig)
layout.addWidget(self.canvas)
self.addToolBar(QtCore.Qt.BottomToolBarArea,
NavigationToolbar(self.canvas, self))
self._axs = self.fig.subplots(2, 1)
self._timer = self.canvas.new_timer(
100, [(self._update_canvas, (), {})])
self._timer.start()
def _update_canvas(self):
[ax.clear() for ax in self._axs]
t = np.linspace(0, 10, 501)
self._axs[0].plot(t, np.tan(t + time.time()), ".")
t = np.linspace(0, 10, 101)
self._axs[1].plot(t, np.sin(t + time.time()))
self.canvas.draw()
if __name__ == "__main__":
qapp = QtWidgets.QApplication(sys.argv)
app = ApplicationWindow()
app.show()
qapp.exec_()

pyQt imshow is not updating

I am trying to update a matrix and show it using Qt with imshow, but it is not updating and I get a warning that the variable room is shadowing another room.
This is my code in Qt using imshow:(After defining the required libraries, the matrix "room" initializes as zero. Then a form designed in Qt Designer, with a widget called "mplt_widget" and a push button called "start_PB")
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout
from PyQt5 import QtCore
import os
import sys
import numpy as np
from time import sleep
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigCanv
# Initializing existence of wave in the room (0)
room = [[0.0 for mm in range(0, 100)] for nn in range(0, 100)]
plt.ion()
Form = uic.loadUiType(os.path.join(os.getcwd(), "FTDT.ui"))[0]
class PlotWin(QMainWindow, Form):
def __init__(self):
QMainWindow.__init__(self)
Form.__init__(self)
self.setupUi(self)
self.fig = plt.figure(frameon=True)
self.ax = Axes3D(self.fig)
self.canvas = FigCanv(self.fig)
self.line1 = []
self.line1.append(self.ax.imshow(room))
l = QVBoxLayout(self.mplt_widget)
l.addWidget(self.canvas)
self.PlotThread = PlotThread(0)
self.start_PB.clicked.connect(self.start)
def start(self):
num = np.random.random()
self.PlotThread = PlotThread(num)
self.PlotThread.update_trigger.connect(self.update_plot)
self.PlotThread.start()
def update_plot(self, room):
self.line1.set_data(room)
self.canvas.draw()
class PlotThread(QtCore.QThread):
update_trigger = QtCore.pyqtSignal(np.ndarray, np.ndarray)
finished_trigger = QtCore.pyqtSignal()
def __init__(self, decay):
QtCore.QThread.__init__(self)
self.decay = decay
def run(self):
for Time in range(10):
for mm in range(0, 100):
for nn in range(0, 100):
room[mm][nn] += self.decay*(room[mm][nn]/2 + 1/2)
self.update_trigger.emit(room)
sleep(1)
self.finished_trigger.emit()
app = QApplication(sys.argv)
app.setStyle("Fusion")
w = PlotWin()
w.show()
sys.exit(app.exec_())

Embed a plot into GUI

I found a website scikit-rf
http://scikit-rf.readthedocs.io/en/latest/tutorials/plotting.html
It can create smith plot by following code
ntwk = rf.Network("my data path")
ntwk.plot_s_smith()
Then I want to embed it to my python gui.
import sys
from PyQt4 import QtGui,QtCore
import matplotlib
import matplotlib.figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
import skrf as rf
import numpy as np
class PrettyWidget(QtGui.QMainWindow):
def __init__(self):
super(PrettyWidget, self).__init__()
self.setGeometry(100,100,1200,700)
self.center()
self.setWindowTitle('NRW')
self.initUI()
def initUI(self):
grid = QtGui.QGridLayout()
self.setLayout(grid)
self.figure = matplotlib.figure.Figure()
self.canvas = FigureCanvas(self.figure)
self.toolbar = NavigationToolbar(self.canvas, self)
grid.addWidget(self.canvas, 3,0,2,12)
def plot1(self):
self.figure.clf()
self.figure.tight_layout()
self.canvas.draw()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
app.aboutToQuit.connect(app.deleteLater)
GUI = PrettyWidget()
GUI.show()
app.exec_()
But I have no idea how to put or modify code below
ntwk = rf.Network("my data path")
ntwk.plot_s_smith()
Would anyone tell me if you have any iedas? Thanks!
Network.plot_s_smith has an ax keyword to plot on existing Axes object. So you can modify your plot function like this:
def plot1(self):
self.figure.clf()
ntwk = rf.Network("my data path")
ntwk.plot_s_smith(ax=self.figure.gca())
self.figure.tight_layout()
self.canvas.draw()

Categories

Resources