其实你很棒 2023-01-02 22:50 采纳率: 83.3%
浏览 115
已结题

PySide6 加载matplotlib

PySide6 加载matplotlib,我计划用PySide6中的graphicsView控件绘图,但图是用matplotlib绘制的,matplotlib绘制的图在graphicsView控件中显示。求一个完整的,可运行,有备注的代码。

  • 写回答

6条回答 默认 最新

  • |__WhoAmI__| 2023-01-02 23:06
    关注

    这是一个使用 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_()
    

    仅供参考,望采纳,谢谢。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(5条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月5日
  • 已采纳回答 1月5日
  • 创建了问题 1月2日

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程