Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9079627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:52:45+00:00 2026-06-16T19:52:45+00:00

Using the python library matplotlib, I’ve found what suggests to be a solution to

  • 0

Using the python library matplotlib, I’ve found what suggests to be a solution to this question:
Displaying (nicely) an algebraic expression in PyQt by utilising matplotlibs TeX markup.

What I’d like to do is take TeX code from my python program which represents a mathematical expression, and save it to an image that can be displayed in my PyQt GUI, rather than displaying the equation in ugly plain text.

Something like this essentially…

import matplotlib.pyplot as plt
formula = '$x=3^2$'

fig = plt.figure()
fig.text(0,0,formula)
fig.savefig('formula.png')

However, the pyplot module is primarily for displaying graphs and plots, not the samples of text like I need. The result of that code is usually a tiny bit of text in the bottom left corner of a huge, white, blank image.
If my formula involves fractions, and thus requires downward space, it is truncated, like in the image below.

Note that this appears a blank image; look to the left side of the display
Fraction at coordinate (0,0) truncated and surrounded by whitespace

I believe I could create a very large (space wise) figure, write the formula in the middle of the blank plot, save it, and use pixel analysis to trim it to as small an image as possible, but this seems rather crude.

Are plots the only intended output of matplotlib?
Is there nothing devoted to just outputting equations, that won’t require me to worry about all the extra space or position of the text?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:52:47+00:00Added an answer on June 16, 2026 at 7:52 pm

    The trick is to render the text, then get its bounding box, and finally adjust the figure size and the vertical positioning of text in the new figure. This saves the figure twice, but as is common in any text engine, the correct bounding box and other parameters can only be correctly obtained after the text has been rendered.

    import pylab
    
    formula = r'$x=3^2, y = \frac{1}{\frac{2}{3}}, %s$' % ('test' * 20)
    
    fig = pylab.figure()
    text = fig.text(0, 0, formula)
    
    # Saving the figure will render the text.
    dpi = 300
    fig.savefig('formula.png', dpi=dpi)
    
    # Now we can work with text's bounding box.
    bbox = text.get_window_extent()
    width, height = bbox.size / float(dpi) + 0.005
    # Adjust the figure size so it can hold the entire text.
    fig.set_size_inches((width, height))
    
    # Adjust text's vertical position.
    dy = (bbox.ymin/float(dpi))/height
    text.set_position((0, -dy))
    
    # Save the adjusted text.
    fig.savefig('formula.png', dpi=dpi)
    

    The 0.005 constant was added to width and height because, apparently, for certain texts Matplotlib is returning a slightly underestimated bounding box, i.e., smaller than required.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the Python library Minimatic found at this site: Minimatic What this essentially
I'm using a open-source Python library in my project. This library logs a lot
I am using python lxml library to parse html pages: import lxml.html # this
I am using this python library that implements the Aho-Corasick string searching algorithm that
I am using the python library rdflib to query Freebase for topics. I found
Python beginner here. I am using the matplotlib library to make graphs from tab
Using Python Image Library PIL and Google App Engine Blobstore... This: img = images.Image(blob_key=image)
I am using Python's AWeber API (https://github.com/aweber/AWeber-API-Python-Library), and I frequently get these exceptions. I
I am attempting to get synonyms for a word using the python library NLTK.
I'm using Python's suds library which tries to fetch xml.xsd over the network. Unfortunately,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.