Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pyqt expressions

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 math 
Python :: How can one find the three largest values of an input array efficiently, namely without having to sort the input array? 
Python :: python wait until 
Python :: python system of equations 
Python :: python time function duration and memory usage 
Python :: add static file in django 
Python :: how to know if the numbers is par in python 
Python :: check pip installed packages inside virtualenv 
Python :: dataframe to dictionary without index 
Python :: kill turtle 
Python :: coco.py 
Python :: isprime in python 
Python :: chi square test in python 
Python :: python: check type and ifno of a data frame 
Python :: how to auto update chromedriver selenium python 
Python :: sqlalchemy if a value in list of values 
Python :: ignition create dataset 
Python :: python read live radio 
Python :: python faker 
Python :: fastapi upload image PIL 
Python :: add y axis label matplotlib 
Python :: how to check python version in cmd 
Python :: python order 2d array by secode element 
Python :: how to create data dictionary in python using keys and values 
Python :: python multi line print 
Python :: python open folder 
Python :: python get lines from text file 
Python :: python thread with parameters 
Python :: getting pi in python 
Python :: convert image to matrix python 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =