可以使用 Matplotlib 库中的 FigureCanvasQTAgg 类将 Matplotlib 图像显示在 PyQt 界面上。首先,需要导入 Matplotlib 库和 PyQt 库。然后,使用 Matplotlib 库绘制图像,并使用 FigureCanvasQTAgg 将图像显示在 PyQt 界面上。
示例代码:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import numpy as np
class MyWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle("Matplotlib in PyQt Example")
# Create a figure and axes
self.figure = Figure()
self.axes = self.figure.add_subplot(111)
# Create a canvas and add the figure to it
self.canvas = FigureCanvas(self.figure)
# Add the canvas to the window
self.setCentralWidget(self.canvas)
# Plot something on the axes
x = np.linspace(0, 10, 100)
self.axes.plot(x, np.sin(x))
app = QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())
这是一个简单的示例,可以在 PyQt 界面中显示一个简单的 Matplotlib 曲线图。