0

So i have a matplotlib plot that is completely black with all axes disabled, the only thing present on the plot is white text, containing a mathematical expression, entered by the user, which is passed into the render function as the parameter 'expr'.

def render(expr, path, width, x, y):
    fig = plt.figure(figsize=(width, 16  if 'begin' in expr else 12))
    plt.text(x, y, expr, fontsize=320, fontweight='bold', color='white')
    fig.set_facecolor('#000000')
    plt.axis('off')
    plt.savefig(path, facecolor=fig.get_facecolor())
    plt.clf()

'width' is the width of the plot, currently i have an unintelligent system in place that strikes a relationship between the length of the plaintext expression and the width of the plot, but it does not work exactly especially with a matrix plot which has multiple lines, the system in place obviously doesn't work very well at all.

I want the required width of the plot to be calculated based on how much the white text stretches across the plot, so as soon as the text finishes widthwise, i want the plot to end there, is there a way of doing this?/ detecting when the white text would come to an end on the plot?

This is so that there is not a lot of black space after the expression, slowing down rendering time to .png and wasting space. As the plot is the same width as the expression

Lewis Kelsey
  • 4,129
  • 1
  • 32
  • 42

1 Answers1

0

There is a full working solution available on github:

Plot a LaTeX equation as a matplotlib figure by ahwillia

https://gist.github.com/ahwillia/c7e54f875913ebc3de3852e9f51ccc69


There is also another module available on github which (also) works on the command line:

texscrap - Python utility for making PDFs out of scraps of LaTeX by johnjosephhorton

https://github.com/johnjosephhorton/texscrap

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712