matplotlib.mathtext显示一个简单的分数(如图像)

时间:2017-09-05 18:17:16

标签: python matplotlib

enter image description here

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

1 个答案:

答案 0 :(得分:0)

确实可以使用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)

输出为

enter image description here