Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pyqt display math

import sys
import matplotlib as mpl
from matplotlib.backends.backend_agg import FigureCanvasAgg
from PySide import QtGui, QtCore

def mathTex_to_QPixmap(mathTex, fs):

    #---- set up a mpl figure instance ----

    fig = mpl.figure.Figure()
    fig.patch.set_facecolor('none')
    fig.set_canvas(FigureCanvasAgg(fig))
    renderer = fig.canvas.get_renderer()

    #---- plot the mathTex expression ----

    ax = fig.add_axes([0, 0, 1, 1])
    ax.axis('off')
    ax.patch.set_facecolor('none')
    t = ax.text(0, 0, mathTex, ha='left', va='bottom', fontsize=fs)

    #---- fit figure size to text artist ----

    fwidth, fheight = fig.get_size_inches()
    fig_bbox = fig.get_window_extent(renderer)

    text_bbox = t.get_window_extent(renderer)

    tight_fwidth = text_bbox.width * fwidth / fig_bbox.width
    tight_fheight = text_bbox.height * fheight / fig_bbox.height

    fig.set_size_inches(tight_fwidth, tight_fheight)

    #---- convert mpl figure to QPixmap ----

    buf, size = fig.canvas.print_to_buffer()
    qimage = QtGui.QImage.rgbSwapped(QtGui.QImage(buf, size[0], size[1],
                                                  QtGui.QImage.Format_ARGB32))
    qpixmap = QtGui.QPixmap(qimage)

    return qpixmap
Comment

PREVIOUS NEXT
Code Example
Python :: pyqt5 display math 
Python :: How to find the three largest values of an array efficiently, in Python? 
Python :: python subtract one list from another 
Python :: python opencv open camera 
Python :: python check variable is tuple 
Python :: registering static files in jango 
Python :: take first n row of dictionary python 
Python :: how to get user ip in python 
Python :: vscode not recognizing python import 
Python :: delete turtle 
Python :: with python how to check alomost similar words 
Python :: pandas to tensor torch 
Python :: fiel to base64 python 
Python :: python sort 2d list 
Python :: getting image from path python 
Python :: pycharm remove not in use imports 
Python :: minute range python 
Python :: python read music stream 
Python :: pandas new df from groupby 
Python :: check if number in range 
Python :: index of sorted list python 
Python :: amazon cli on commadline 
Python :: make pandas df from np array 
Python :: Parameter Grid python 
Python :: on message discord py 
Python :: python open folder in explorer 
Python :: python read lines from text file 
Python :: from django.conf.urls import patterns 
Python :: print() in python 
Python :: pipenv 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =