Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert matplotlib figure canvas image into QTextDocument

I am creating an app which calculates grades and assigns grade letters automatically, within the app the plots are created using Matplotlib. I would like to include the QTableView and the figures into a QTextDocument for printing. I know how to insert QTableView, but I have no idea how to insert the figures after the table.

enter image description here

Is this possible directly or should the images be saved in the disk first?

like image 836
Khalil Al Hooti Avatar asked Jul 04 '26 09:07

Khalil Al Hooti


1 Answers

You have to convert the canvas to QImage and then add it as a resource to the QTextDocument.

import sys
import time
import uuid

import numpy as np

from matplotlib.backends.qt_compat import QtCore, QtGui, QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvas
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)))
        layout.addWidget(static_canvas)
        static_canvas.setFixedHeight(200)

        self._static_ax = static_canvas.figure.subplots()
        t = np.linspace(0, 10, 501)
        self._static_ax.plot(t, np.tan(t), "r.")

        self.textedit = QtWidgets.QTextEdit()
        button = QtWidgets.QPushButton("Add Canvas")
        button.clicked.connect(self.add_image)
        layout.addWidget(button, stretch=0)
        layout.addWidget(self.textedit, stretch=1)

    def add_image(self):
        document = self.textedit.document()

        img = self.canvasToQImage(self._static_ax.figure.canvas)
        url = QtCore.QUrl()
        url.setScheme("mydata")
        url.setHost("image-{uuid}.png".format(uuid=uuid.uuid4()))
        document.addResource(QtGui.QTextDocument.ImageResource, url, img)

        # add image
        cursor = QtGui.QTextCursor(document)
        imageFormat = QtGui.QTextImageFormat()
        imageFormat.setName(url.toString())
        cursor.insertImage(imageFormat)

        # or
        # self.textedit.append('<img src="{url}" />'.format(url=url.toString()))

    @staticmethod
    def canvasToQImage(canvas):
        data = canvas.buffer_rgba()
        ch = 4
        w, h = canvas.get_width_height()
        bytesPerLine = ch * w
        img = QtGui.QImage(data, w, h, bytesPerLine, QtGui.QImage.Format_ARGB32)
        return img.rgbSwapped()


if __name__ == "__main__":
    qapp = QtWidgets.QApplication(sys.argv)
    app = ApplicationWindow()
    app.resize(640, 480)
    app.show()
    qapp.exec_()
like image 166
eyllanesc Avatar answered Jul 06 '26 22:07

eyllanesc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!