@warvariuc Python 3 . , . -, StringIO cStringIO Py3. -, io.StringIO , , MatPlotLib. : http://matplotlib.1069221.n5.nabble.com/savefig-and-StringIO-error-on-Python3-td44241.html. , , io.BytesIO. getvalue() , StringIO. savefig , , :
from io import BytesIO
import matplotlib.pyplot as plt
def renderLatex(formula, fontsize=12, dpi=300, format='svg', file=None):
"""Renders LaTeX formula into image or prints to file.
"""
fig = plt.figure(figsize=(0.01, 0.01))
fig.text(0, 0, u'${}$'.format(formula), fontsize=fontsize)
output = BytesIO() if file is None else file
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=MathTextWarning)
fig.savefig(output, dpi=dpi, transparent=True, format=format,
bbox_inches='tight', pad_inches=0.0, frameon=False)
plt.close(fig)
if file is None:
output.seek(0)
return output
The warning was something that I'm sure is related to the size of the figure. You can remove the attached withif you want. The reason for the search is to make the "file" readable (best explained here: fooobar.com/questions/207621 / ... ).
source
share