Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to display a Sympy matrix in pyqt5?
Qt 6.11 is out! See what's new in the release blog

How to display a Sympy matrix in pyqt5?

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 1.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rwestga
    wrote on last edited by
    #1

    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-pyqt

    This 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 message

    The 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 output

    Code 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()
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved