Print latex-formula with python

2020-02-25 08:29发布

How to show an easy latex-formula in python? Maybe numpy is the right choice?

EDIT:

I have python code like:

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

and want to print this in a graphical output (like matplotlib).

5条回答
2楼-- · 2020-02-25 08:57

As suggested by Andrew little work around using 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楼-- · 2020-02-25 09:02

Draw with 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

查看更多
女痞
4楼-- · 2020-02-25 09:08

Without ticks:

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)
查看更多
祖国的老花朵
5楼-- · 2020-02-25 09:14

Matplotlib can already do TeX, by setting text.usetex: True in ~/.matplotlib/matplotlibrc. Then, you can just use TeX in all displayed strings, e.g.,

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

(be sure to use the $ as in normal in-line TeX!). The r before the string means that no substitutions are made; otherwise you have to escape the slashes as mentioned.

More info at the matplotlib site.

查看更多
欢心
6楼-- · 2020-02-25 09:23

Creating mathematical formulas in Pandas.

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

查看更多
登录 后发表回答