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?
Related
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')
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_())
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_()
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_())
I'm trying to develop a very simple pyside/Qt program using matplotlib. I want that a graph is draw when a button is pressed. So far, I can draw something on the constructor, but I can't connect the Pyside event with matplotlib. Is there a way to do that?
import sys
import platform
import numpy as np
import PySide
from PySide.QtGui import QApplication, QMainWindow, QTextEdit,\
QPushButton, QMessageBox, QWidget, QVBoxLayout
from PySide import QtCore
__version__ = '0.0.1'
import matplotlib
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.main_frame = QWidget()
self.figure = Figure()
self.canvas = FigureCanvas(self.figure)
self.canvas.setParent( self.main_frame )
self.axes = self.figure.add_subplot(111)
vbox = QVBoxLayout( )
vbox.addWidget( self.canvas )
self.main_frame.setLayout( vbox )
self.setCentralWidget( self.main_frame )
self.button = QPushButton('Run')
def button_pressed(self):
data1 = np.loadtxt('FStream.dat')
data2 = np.loadtxt('FShield.dat')
self.axes.plot(data1[0],data1[1],data2[0],data2[1])
print 'pressed'
self.canvas.draw()
if __name__ == '__main__':
app = QApplication(sys.argv)
frame = MainWindow()
frame.button.clicked.connect(frame.button_pressed)
frame.button.show()
frame.show()
app.exec_()
Thanks a lot!
EDIT: edited the code to put the draw().
EDIT 2: Separating in different functions is now looking like this:
import sys
import platform
import numpy as np
import PySide
from PySide.QtGui import QApplication, QMainWindow, QTextEdit,\
QPushButton, QMessageBox, QWidget, QVBoxLayout
from PySide import QtCore
__version__ = '0.0.1'
from ui_pygradient_uni import Ui_MainWindow
import matplotlib
import widgets.matplotlibwidget
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
#from matplotlib.figure import Figure
#from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.main_frame = widgets.matplotlibwidget.MatplotlibWidget()
self.button = QPushButton('Run')
vbox = QVBoxLayout( )
vbox.addWidget( self.main_frame.canvas )
self.main_frame.setLayout( vbox )
self.setCentralWidget( self.main_frame )
def button_pressed(self):
data1 = np.loadtxt('FStream.dat')
data2 = np.loadtxt('FShield.dat')
self.axes.plot(data1[0],data1[1],data2[0],data2[1])
self.canvas.draw()
print 'pressed'
if __name__ == '__main__':
app = QApplication(sys.argv)
frame = MainWindow()
frame.button.clicked.connect(frame.main_frame.Plot)
frame.button.show()
frame.show()
app.exec_()
And the matplotlibwidget is like this:
import matplotlib
import numpy as np
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
class MatplotlibWidget(FigureCanvas):
def __init__(self, parent=None):
super(MatplotlibWidget, self).__init__(Figure())
self.setParent(parent)
self.figure = Figure()
self.canvas = FigureCanvas(self.figure)
self.axes = self.figure.add_subplot(111)
def Plot(self):
data1 = np.loadtxt('FStream.dat')
data2 = np.loadtxt('FShield.dat')
self.axes.plot(data1[0],data1[1],data2[0],data2[1])
self.canvas.draw()
Try putting in
self.canvas.draw()
after
self.axes.plot(data1[0],data1[1],data2[0],data2[1])
Your GUI code looks ok and button_pressed is being called correctly but the graph isn't being redrawn.