How to display a Sympy matrix in pyqt5?
-
wrote on 27 Jun 2021, 14:10 last edited by
I am working on an application for which I want to include the functionality to display matrices to users.
I have modified code from the following link to display equations:
https://stackoverflow.com/questions/14097463/displaying-nicely-an-algebraic-expression-in-pyqtThis works well for displaying equations (commented out in code). For displaying a matrix I can't get it to work though. I get the following error:
error messageThe example matrix looks like this in Jupyter Notebook. I'd like the output in pyqt5 to look the same. I'd like to know if there is a way to get this method to work, if not, is there an alternative (simple) approach?
jupyter matrix outputCode to run the problem:
import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QComboBox, QPushButton, QVBoxLayout from PyQt5.QtGui import QPainter, QPen from PyQt5.QtCore import Qt from matplotlib.figure import Figure from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas import sympy from sympy import* class Pop_Up(QtWidgets.QWidget): def __init__(self): super().__init__() self.setGeometry(50,50,600,300) self.setWindowTitle("Test") self.centralwidget = QtWidgets.QWidget(self) self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget) self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 500, 250)) self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget) self.verticalLayout.setContentsMargins(0, 0, 0, 0) self._figure=Figure() self._canvas=FigureCanvas(self._figure) self.verticalLayout.addWidget(self._canvas) def paintEvent(self,event): #equation strings Swedish_rolling=r'$[sin(\alpha + \beta + \gamma) , -cos(\alpha + \beta + \gamma) , -lcos(\beta + \gamma)] R(\theta) \dot{\xi}_I - r\dot{\varphi}cos(\gamma) = 0$' #Matrix input: theta = symbols('theta') Matrix1 = Matrix([[cos(theta),-sin(theta),0],[sin(theta),cos(theta),0],[0,0,1]]) #clears or resets self._figure.clear() #swap output strings to compare equation and matrix output #outputStr = Swedish_rolling outputStr = r"$" + f"{sympy.latex(Matrix1)}" + r"$" text=self._figure.suptitle( outputStr, x=0.0, y=1.0, horizontalalignment='left', verticalalignment='top', size=QtGui.QFont().pointSize()*1 ) self._canvas.draw() def window(): app=QApplication(sys.argv) win = Pop_Up() win.show() sys.exit(app.exec_()) window()
-
Hi and welcome to devnet,
Qwt's QwtText class might be of interest.
Note that this is a C++ library but there are likely Python bindings for it.
1/2