Matplotlib save only text without whitespace

Viewed 1216

So in python I have the following code, taken from this answer:

import matplotlib.pyplot as plt                                                 
import sympy                                                                    

x = sympy.symbols('x')                               
y = 1 + sympy.sin(sympy.sqrt(x**2 + 20))                                         
lat = sympy.latex(y)                                                            

#add text                                 
plt.text(0, 0.6, r"$%s$" % lat, fontsize = 50)                                  

#hide axes                                                                      
fig = plt.gca()                                                                 
fig.axes.get_xaxis().set_visible(False)                                         
fig.axes.get_yaxis().set_visible(False)                                         
plt.savefig('out.png', bbox_inches='tight', pad_inches=0)                                                        
plt.show()

This opens the text and exports it to a png file just fine:

Image Export 1

But this includes whitespace beyond the whitespace outside of the frame. How would you go about cropping the image to export only the text, like a bounding box, like so?

enter image description here

1 Answers
Related