![]() |
PyQt + Matplotlib - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: PyQt + Matplotlib (/thread-36790.html) Pages:
1
2
|
PyQt + Matplotlib - frohr - Mar-30-2022 Hi, I have window with buttons and some text and I need insert matplotlib below buttons. Here is my code: import sys from PyQt6.QtCore import QSize, Qt from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QCheckBox, QWidget, QLabel from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.figure import Figure import matplotlib.pyplot as plt class AnotherWindow(QWidget): def __init__(self): super().__init__() layout = QVBoxLayout() self.setWindowTitle("MFR - Options") self.setMinimumSize(QSize(800, 400)) self.setLayout(layout) class MainWindow(QMainWindow): def button_TS_clicked(self): print("Time signal button clicked!") def button_FFT1500_clicked(self): print("FFT1500 button clicked!") def button_FFT7500_clicked(self): print("FFT7500 button clicked!") def button_hilbert_clicked(self): print("Hilbert button clicked!") def button_save_clicked(self): print("Save button clicked!") def button_options_clicked(self): self.dialog.exec_() def cb_update(self, state): if state == Qt.CheckState.Checked.value: print("AutoUpdate ON") else: print("AutoUpdate OFF") def options_window(self, checked): if self.window1.isVisible(): self.window1.hide() else: self.window1.show() def __init__(self): super().__init__() self.window1 = AnotherWindow() self.setWindowTitle("MFR") self.setMinimumSize(QSize(1024, 600)) button_TS = QPushButton("Time signal", self) button_TS.move(30,30) button_TS.clicked.connect(self.button_TS_clicked) button_FFT1500 = QPushButton("FFT 10-1500Hz", self) button_FFT1500.move(150,30) button_FFT1500.clicked.connect(self.button_FFT1500_clicked) button_FFT7500 = QPushButton("FFT 500-7500Hz", self) button_FFT7500.move(270,30) button_FFT7500.clicked.connect(self.button_FFT7500_clicked) button_hilbert = QPushButton("Hilbert", self) button_hilbert.move(390,30) button_hilbert.clicked.connect(self.button_hilbert_clicked) button_save = QPushButton("Save wav", self) button_save.move(510,30) button_save.clicked.connect(self.button_save_clicked) button_options = QPushButton("Options", self) button_options.move(630,30) button_options.clicked.connect(self.options_window) cb = QCheckBox("Auto Update", self) cb.move(750,30) cb.stateChanged.connect(self.cb_update) cb.toggle() cb.setChecked(False) self.label_1 = QLabel(self) self.label_1.move(30, 70) self.label_1.setText("SOME TEXTS.........................") app = QApplication(sys.argv) window = MainWindow() window.show() app.exec()I need it like this: https://pasteboard.co/2IfAA0WyUAxX.jpg I tried combine with this code, but I am totally lost. Thank you for help. RE: PyQt + Matplotlib - Axel_Erfurt - Mar-30-2022 An Example for PQt5 import sys import matplotlib matplotlib.use("Qt5Agg") from PyQt5 import QtCore from PyQt5.QtWidgets import QApplication, QMainWindow, QSizePolicy from numpy import arange, sin, pi from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.figure import Figure class MyMplCanvas(FigureCanvas): def __init__(self, parent=None, width=5, height=4, dpi=100): fig = Figure(figsize=(width, height), dpi=dpi) self.axes = fig.add_subplot(111) self.compute_initial_figure() FigureCanvas.__init__(self, fig) self.setParent(parent) FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding) FigureCanvas.updateGeometry(self) def compute_initial_figure(self): pass class MyStaticMplCanvas(MyMplCanvas): def compute_initial_figure(self): t = arange(0.0, 3.0, 0.01) s = sin(2*pi*t) self.axes.plot(t, s) class ApplicationWindow(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.setAttribute(QtCore.Qt.WA_DeleteOnClose) self.setWindowTitle("Window") sc = MyStaticMplCanvas(self, width=5, height=4, dpi=100) self.setCentralWidget(sc) if __name__ == '__main__': app = QApplication(sys.argv) aw = ApplicationWindow() aw.setWindowTitle("PyQt5 Matplot Example") aw.show() app.exec() RE: PyQt + Matplotlib - Axel_Erfurt - Mar-30-2022 or use the builtin chart in PyQt6 from PyQt6.QtWidgets import QApplication, QMainWindow import sys from PyQt6.QtCharts import QChart, QChartView, QLineSeries from PyQt6.QtCore import QPointF from PyQt6.QtGui import QPainter from PyQt6.QtCore import Qt class Window(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("PyQtChart Line") self.setGeometry(100,100, 680,500) self.show() self.create_linechart() def create_linechart(self): series = QLineSeries(self) series << QPointF( 1, 91.9) << QPointF( 2, 89.5) << QPointF( 3, 84.6) << QPointF( 4, 92.0) << QPointF( 5, 87.6) << QPointF( 6, 89.4) << QPointF( 7, 83.9) << QPointF( 8, 90.1) << QPointF( 9, 89.7) << QPointF( 10, 89.6) << QPointF( 11, 86.1) << QPointF( 12, 85.3) << QPointF( 13, 91.3) << QPointF( 14, 89.0) << QPointF( 15, 85.3) << QPointF( 16, 91.6) << QPointF( 17, 91.1) << QPointF( 18, 91.3) << QPointF( 19, 88.3) << QPointF( 20, 84.1) << QPointF( 21, 88.9) << QPointF( 22, 91.1) << QPointF( 23, 86.6) << QPointF( 24, 88.2) << QPointF( 25, 90.3) << QPointF( 26, 89.3) << QPointF( 27, 85.0) << QPointF( 28, 88.6) << QPointF( 29, 86.0) << QPointF( 30, 87.1) << QPointF( 31, 84.3) chart = QChart() chart.addSeries(series) chart.createDefaultAxes() chart.setAnimationOptions(QChart.AnimationOption.SeriesAnimations) chart.setTitle("Line Chart Example") chart.legend().setVisible(True) chart.legend().setAlignment(Qt.AlignmentFlag.AlignBottom) chartview = QChartView(chart) chartview.setRenderHint(QPainter.RenderHint.Antialiasing) self.setCentralWidget(chartview) App = QApplication(sys.argv) window = Window() sys.exit(App.exec()) RE: PyQt + Matplotlib - frohr - Mar-30-2022 Thank you for the code, seems great. And is any possibility to use Matplotlib (because FFT) in PyQt6 like my code? Because I like Qt6 design but I have no clue hot to insert the Plot and make space for buttons on the top. Thank for your help! RE: PyQt + Matplotlib - deanhystad - Mar-30-2022 https://www.pythonguis.com/tutorials/plotting-matplotlib/?msclkid=b7bdb7e3b05411ec841ad8fceb18628b RE: PyQt + Matplotlib - Axel_Erfurt - Mar-30-2022 I don't think matplotlib works properly with PyQt6 yet. RE: PyQt + Matplotlib - frohr - Mar-30-2022 (Mar-30-2022, 06:57 PM)Axel_Erfurt Wrote: I don't think matplotlib works properly with PyQt6 yet. Yes, I see. I will use PyQt5. And how to move plot for example 100pixels down? Thanks RE: PyQt + Matplotlib - Axel_Erfurt - Mar-30-2022 You can add an empty widget with fixed height import sys import matplotlib matplotlib.use("Qt5Agg") from PyQt5 import QtCore from PyQt5.QtWidgets import (QApplication, QMainWindow, QSizePolicy, QLabel, QVBoxLayout, QHBoxLayout, QWidget, QPushButton) from numpy import arange, sin, pi from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.figure import Figure class MyMplCanvas(FigureCanvas): def __init__(self, parent=None, width=5, height=4, dpi=100): fig = Figure(figsize=(width, height), dpi=dpi) self.axes = fig.add_subplot(111) self.compute_initial_figure() FigureCanvas.__init__(self, fig) self.setParent(parent) FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding) FigureCanvas.updateGeometry(self) def compute_initial_figure(self): pass class MyStaticMplCanvas(MyMplCanvas): def compute_initial_figure(self): t = arange(0.0, 3.0, 0.01) s = sin(2*pi*t) self.axes.plot(t, s) class ApplicationWindow(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.setAttribute(QtCore.Qt.WA_DeleteOnClose) self.setWindowTitle("Window") sc = MyStaticMplCanvas(self, width=5, height=4, dpi=100) vbox = QVBoxLayout() box_layout = QWidget() buttonbox = QHBoxLayout() btn = QPushButton("Button 1") btn.clicked.connect(self.btn_1_clicked) buttonbox.addWidget(btn) btn2 = QPushButton("Button 2") buttonbox.addWidget(btn2) btn3 = QPushButton("Button 3") buttonbox.addWidget(btn3) vbox.addLayout(buttonbox) box_layout.setLayout(vbox) label = QLabel("Some Text ...") vbox.addWidget(label) # 100px space space = QWidget() space.setFixedHeight(100) vbox.addWidget(space) vbox.addWidget(sc) self.setCentralWidget(box_layout) self.setGeometry(50, 50, 800, 600) def btn_1_clicked(self): print(f"Button 1 clicked") if __name__ == '__main__': app = QApplication(sys.argv) aw = ApplicationWindow() aw.setWindowTitle("PyQt5 Matplot Example") aw.show() app.exec() RE: PyQt + Matplotlib - Axel_Erfurt - Mar-31-2022 For spectrum analyzer you can use pyqtgraph https://github.com/sbarratt/spectrum-analyzer ![]() RE: PyQt + Matplotlib - frohr - Apr-01-2022 Hi, I started from beginning and now I have no clue how to make tight layout of the plot? Any suggestion please? import sys import matplotlib matplotlib.use('Qt5Agg') from PyQt5 import QtCore, QtGui, QtWidgets from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar from matplotlib.figure import Figure class MplCanvas(FigureCanvasQTAgg): def __init__(self, parent=None, width=5, height=4, dpi=100): fig = Figure(figsize=(width, height), dpi=dpi) self.axes = fig.add_subplot(111) super(MplCanvas, self).__init__(fig) class MainWindow(QtWidgets.QMainWindow): def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) sc = MplCanvas(self, width=5, height=4, dpi=100) sc.axes.plot([0,1,2,3,4], [10,1,20,3,40]) # Create toolbar, passing canvas as first parament, parent (self, the MainWindow) as second. toolbar = NavigationToolbar(sc, self) layout = QtWidgets.QVBoxLayout() layout.addWidget(toolbar) layout.addWidget(sc) # Create a placeholder widget to hold our toolbar and canvas. widget = QtWidgets.QWidget() widget.setLayout(layout) self.setCentralWidget(widget) self.show() app = QtWidgets.QApplication(sys.argv) w = MainWindow() app.exec_()Thanks |