如何在python中显示简单的乳胶配方? 也许numpy是正确的选择?
编辑:
我有python代码,如:
a = '\frac{a}{b}'
并希望在图形输出中打印(如matplotlib)。
答案 0 :(得分:9)
正如安德鲁建议使用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()
答案 1 :(得分:2)
通过在text.usetex: True
中设置~/.matplotlib/matplotlibrc
,Matplotlib已经可以执行TeX了。然后,您可以在所有显示的字符串中使用TeX,例如
ylabel(r"Temperature (K) [fixed $\beta=2$]")
(务必使用正常的在线TeX中的$
!)。字符串前面的r
表示没有替换;否则你必须像上面提到的那样逃避斜线。
matplotlib网站上的更多信息。
答案 2 :(得分:2)
基于this one的特定于Jupyter笔记本的答案,使用f字符串格式化constexpr bool different(const char * x, const char * y)
{
return std::string_view{x} != std::string_view{y};
}
变量:
$x_i$
注意:f-string(带格式的字符串文字)使用大括号插入Python变量from IPython.display import display, Latex
for i in range(3):
display(Latex(f'$x_{i}$'))
的值。您需要double the curly braces(i
)才能在LaTeX代码中实际使用f'{{}}'
。否则,您可以直接在普通的Python字符串(而不是f字符串)中使用单个花括号。
侧面说明::令我惊讶的是,堆栈溢出仍然doesn’t have a math markup。
答案 3 :(得分: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()
答案 4 :(得分: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")
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")
答案 5 :(得分:0)
没有刻度:
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)