PySide6 加载matplotlib,我计划用PySide6中的graphicsView控件绘图,但图是用matplotlib绘制的,matplotlib绘制的图在graphicsView控件中显示。求一个完整的,可运行,有备注的代码。
这是一个使用 PySide6 和 Matplotlib 绘制图形的例子。它使用 Matplotlib 的 FigureCanvas 类将图形绘制到 QWidget 中,然后使用 QGraphicsView 将图形显示出来。
需要先安装 PySide6 和 Matplotlib:
pip install pyside6 matplotlib
import sys
import random
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton
from PySide6.QtCore import Qt
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
class MatplotlibWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
# Create a Matplotlib figure and a FigureCanvas to display it
self.figure = Figure()
self.canvas = FigureCanvas(self.figure)
# Create a QGraphicsView to display the FigureCanvas
self.view = QGraphicsView(self)
self.view.setRenderHint(QGraphicsView.Antialiasing)
self.view.setRenderHint(QGraphicsView.SmoothPixmapTransform)
self.view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.view.setResizeAnchor(QGraphicsView.AnchorViewCenter)
self.view.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
self.view.setOptimizationFlag(QGraphicsView.DontAdjustForAntialiasing, True)
self.view.setOptimizationFlag(QGraphicsView.DontSavePainterState, True)
# Create a QGraphicsScene to hold the FigureCanvas
self.scene = QGraphicsScene(self)
self.scene.addWidget(self.canvas)
self.view.setScene(self.scene)
# Lay out the widget
layout = QVBoxLayout(self)
layout.addWidget(self.view)
self.setLayout(layout)
def plot(self):
# Generate some random data
data = [random.random() for _ in range(10)]
# Clear the figure and redraw the plot
self.figure.clear()
ax = self.figure.add_subplot(111)
ax.plot(data, "r-")
self.canvas.draw()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = QMainWindow()
# Create a MatplotlibWidget and add it to the window
plot_widget = MatplotlibWidget()
window.setCentralWidget(plot_widget)
# Create a button to redraw the plot
button = QPushButton("Redraw")
button.clicked.connect(plot_widget.plot)
window.addToolBar(button)
window.show()
app.exec_()
仅供参考,望采纳,谢谢。
完整的 PySide6 加载 matplotlib 的代码如下,用于在 PySide6 中的 graphicsView 控件中显示 matplotlib 绘制的图:
import matplotlib.pyplot as plt
from PySide6.QtWidgets import QApplication, QGraphicsView, QMainWindow
# 创建 matplotlib 图形
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1, 2, 3, 4, 5], [1, 4, 2, 3, 5])
# 创建 QApplication 实例
app = QApplication()
# 创建 QGraphicsView 控件
view = QGraphicsView()
# 将 matplotlib 图形加载到 QGraphicsView 中
view.setCentralWidget(fig.canvas)
# 创建 QMainWindow 窗口并将 QGraphicsView 控件作为中心窗口部件
window = QMainWindow()
window.setCentralWidget(view)
window.show()
# 运行应用程序
app.exec_()
一楼的我在研究研究,全是英文备注
ATools PySide6版本依赖包
借鉴下
https://blog.csdn.net/zy1620454507/article/details/127246396
使用 graphicsView 控件显示 matplotlib 绘制的图的完整代码参考
import random
import numpy as np
from PySide6 import QtGui, QtWidgets, QtCore
from matplotlib.backends.backend_qt6agg import FigureCanvas, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
# 创建 matplotlib 的 Figure 和 Canvas
self.figure = Figure()
self.canvas = FigureCanvas(self.figure)
# 创建 NavigationToolbar,并添加到窗口中
self.toolbar = NavigationToolbar(self.canvas, self)
self.addToolBar(self.toolbar)
# 创建布局,并将 Canvas 添加到布局中
self.layout = QtWidgets.QVBoxLayout()
self.layout.addWidget(self.canvas)
# 创建 CentralWidget,并将布局添加到 CentralWidget 中
self.central_widget = QtWidgets.QWidget()
self.central_widget.setLayout(self.layout)
self.setCentralWidget(self.central_widget)
# 绘制图形
self.plot()
def plot(self):
# 获取 Figure 和 Axes 对象
figure = self.figure
ax = figure.add_subplot()
# 生成数据
x = np.linspace(0, 10, 100)
y = x ** 2
# 绘制折线图
ax.plot(x, y)
if __name__ == '__main__':
app = QtWidgets.QApplication()
window = MainWindow()
window.show()
app.exec_()
使用了 matplotlib 的 Figure 和 Canvas 对象来绘制图形,并使用 NavigationToolbar 控件提供缩放、平移、保存等功能。然后使用 PySide6 的 QVBoxLayout 布局将 Canvas 和 NavigationToolbar 添加到窗口中。最后使用 matplotlib 的 Axes 对象绘制折线图。
本代码使用了 PySide6,如果使用的是 PyQt6,需要将 from PySide6 import QtGui, QtWidgets, QtCore 替换为 from PyQt6 import QtGui, QtWidgets, QtCore。
下面是一个使用PySide6加载matplotlib并在graphicsView控件中显示图像的完整代码示例:
import sys
from PySide6 import QtWidgets, QtGui
from PySide6.QtWidgets import QApplication, QMainWindow, QGraphicsView, QGraphicsScene
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt6agg import FigureCanvasQTAgg as FigureCanvas
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 创建图形视图控件
self.view = QGraphicsView(self)
self.view.setGeometry(10, 10, 400, 300)
# 创建图形场景
self.scene = QGraphicsScene(self)
self.view.setScene(self.scene)
# 创建matplotlib图形
self.figure = Figure()
self.canvas = FigureCanvas(self.figure)
# 将matplotlib图形添加到图形场景中
self.scene.addWidget(self.canvas)
# 绘制图形
self.plot()
def plot(self):
# 创建一个子图
ax = self.figure.add_subplot(111)
# 绘制一条折线图
ax.plot([1, 2, 3, 4, 5], [1, 4, 2, 3, 5])
# 更新图形
self.canvas.draw()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())