메모
전체 예제 코드를 다운로드 하려면 여기 를 클릭 하십시오.
Qt에 임베딩 #
Matplotlib 캔버스를 포함하는 간단한 Qt 애플리케이션. 이 프로그램은 모든 Qt 바인딩(PyQt6, PySide6, PyQt5, PySide2)을 사용하여 동일하게 잘 작동합니다. QT_API
환경 변수를 바인딩 이름으로 설정하거나 먼저 가져와서 바인딩을 선택할 수 있습니다 .
import sys
import time
import numpy as np
from matplotlib.backends.qt_compat import QtWidgets
from matplotlib.backends.backend_qtagg 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)))
# Ideally one would use self.addToolBar here, but it is slightly
# incompatible between PyQt6 and other bindings, so we just add the
# toolbar as a plain widget instead.
layout.addWidget(NavigationToolbar(static_canvas, self))
layout.addWidget(static_canvas)
dynamic_canvas = FigureCanvas(Figure(figsize=(5, 3)))
layout.addWidget(dynamic_canvas)
layout.addWidget(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()
t = np.linspace(0, 10, 101)
# Set up a Line2D.
self._line, = self._dynamic_ax.plot(t, np.sin(t + time.time()))
self._timer = dynamic_canvas.new_timer(50)
self._timer.add_callback(self._update_canvas)
self._timer.start()
def _update_canvas(self):
t = np.linspace(0, 10, 101)
# Shift the sinusoid as a function of time.
self._line.set_data(t, np.sin(t + time.time()))
self._line.figure.canvas.draw()
if __name__ == "__main__":
# Check whether there is already a running QApplication (e.g., if running
# from an IDE).
qapp = QtWidgets.QApplication.instance()
if not qapp:
qapp = QtWidgets.QApplication(sys.argv)
app = ApplicationWindow()
app.show()
app.activateWindow()
app.raise_()
qapp.exec()