如何使用Python渲染Latex标记?

问题描述 投票:12回答:5

如何在python中显示简单的乳胶配方?也许numpy是正确的选择?

编辑:

我有类似python的代码:

a = '\frac{a}{b}'

并且要在图形输出中将其打印出来(例如matplotlib)。

python latex formula
5个回答
10
投票

根据安德鲁的建议,使用matplotlib的工作很少。

import matplotlib.pyplot as plt
a = '\\frac{a}{b}'  #notice escaped slash
plt.plot()
plt.text(0.5, 0.5,'$%s$'%a)
plt.show()

3
投票

Matplotlib已经可以通过在text.usetex: True中设置~/.matplotlib/matplotlibrc来执行TeX。然后,您可以在所有显示的字符串中使用TeX,例如

ylabel(r"Temperature (K) [fixed $\beta=2$]")

((请确保像普通的嵌入式TeX一样使用$!)。字符串前的r表示不进行任何替换;否则,您必须如上所述避免斜线。

matplotlib网站上的更多信息。


2
投票

无刻度:

a = r'\frac{a}{b}'
ax = plt.axes([0,0,0.1,0.2]) #left,bottom,width,height
ax.set_xticks([])
ax.set_yticks([])
plt.text(0.3,0.4,'$%s$' %a,size=40)

1
投票

使用matplotlib绘制,

import matplotlib.pyplot as plt
a = r'\frac{a}{b}'
ax=plt.subplot(111)
ax.text(0.5,0.5,r"$%s$" %(a),fontsize=30,color="green")
plt.show()

enter image description here


1
投票

在熊猫中创建数学公式。

a = r'\frac{a}{b}'
ax = plt.axes([0,0,0.3,0.3]) #left,bottom,width,height
ax.set_xticks([])
ax.set_yticks([])
ax.axis('off')
plt.text(0.4,0.4,'$%s$' %a,size=50,color="green")

enter image description here

a = r'f(x) = \frac{\exp(-x^2/2)}{\sqrt{2*\pi}}'
ax = plt.axes([0,0,0.3,0.3]) #left,bottom,width,height
ax.set_xticks([])
ax.set_yticks([])
ax.axis('off')
plt.text(0.4,0.4,'$%s$' %a,size=50,color="green")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.