Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pyqt pylatex

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 :: how to change the datatype of a row in pandas 
Python :: python wait until 
Python :: create directory in python 
Python :: dice roller python 
Python :: convert string representation of a list to list 
Python :: add percentage column pandas 
Python :: remove whitespace in keys from dictionary 
Python :: sklearn accuracy 
Python :: python read png file 
Python :: flask post vs get 
Python :: python pandas cumulative sum of column 
Python :: How can I get terminal output in python 
Python :: python print return code of requests 
Python :: python last element of list 
Python :: add header to table in pandas 
Python :: Consider using python 3 style super without arguments 
Python :: convert categorical column to int in pandas 
Python :: change the color of the button on hovering tkinter 
Python :: show a image in python 
Python :: how to join a list of characters in python 
Python :: python dataframe column string to integer python 
Python :: encrypt and decrypt python 
Python :: or statement django template 
Python :: how to get location of word in list in python 
Python :: convert any base to decimal python 
Python :: how to log ip addresses in django 
Python :: python import ndjson data 
Python :: quit button tkinter 
Python :: python format decimal 
Python :: how to import numpy array in python 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =