Python matplotlib.mathtext显示简单分数(如图所示)

Python matplotlib.mathtext显示简单分数(如图所示),python,matplotlib,Python,Matplotlib,如何在mathtext中创建这个数学表达式,并将其存储为图像(png)以供以后在tkinter应用程序中使用? (编辑:它不必是matplotlib,顺便说一句,任何能够完成这项工作的python库都可以!)一个可能的解决方案确实是使用matplotlib。您可以将mathtext作为文本放入图形中,并将其保存为png,将保存的区域限制为文本区域 import matplotlib.pyplot as plt # create MathText formula formula = r"$\fr

如何在mathtext中创建这个数学表达式,并将其存储为图像(png)以供以后在tkinter应用程序中使用?
(编辑:它不必是matplotlib,顺便说一句,任何能够完成这项工作的python库都可以!)

一个可能的解决方案确实是使用matplotlib。您可以将mathtext作为文本放入图形中,并将其保存为png,将保存的区域限制为文本区域

import matplotlib.pyplot as plt

# create MathText formula
formula = r"$\frac{15.118\times 100}{460.370} = 3.3 \%$"

#make a plot
fig, ax = plt.subplots()
# add formula as text
tx = ax.text(0.5,0.5,formula, ha="center", va="center")
# turn axes off
ax.axis("off")

# draw the canvas, such that a renderer is defined
fig.canvas.draw()
# obtain the extent of the formula in inches
trans = tx.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
# save the figure, limited to the formula's extent
plt.savefig(__file__+".png", bbox_inches=trans)
输出将是


你试过用谷歌搜索“mathtext matplotlib”吗?没错,我想不出来。有可能吗?我读过这篇文章,准确地说:太好了!你将如何在分数中的单词之间添加空格?Like:formula=r“$\frac{'space>MathText的工作范围与latex完全相同,可以接受大多数latex命令。To或To有几种不同的方式,您可以选择最方便的方式。