Dan*_*Dan 6 python matplotlib tex python-2.4
我在我的python脚本中有一个数字,我想用它作为matplotlib中一个情节标题的一部分.是否有一个函数将float转换为格式化的TeX字符串?
基本上,
str(myFloat)
Run Code Online (Sandbox Code Playgroud)
回报
3.5e+20
Run Code Online (Sandbox Code Playgroud)
但我想要
$3.5 \times 10^{20}$
Run Code Online (Sandbox Code Playgroud)
或者至少对于matplotlib来格式化浮点数,就像格式化第二个字符串一样.我也使用python 2.4,所以特别赞赏在旧版本中运行的代码.
使用旧的格式化:
print r'$%s \times 10^{%s}$' % tuple('3.5e+20'.split('e+'))
Run Code Online (Sandbox Code Playgroud)
新格式:
print r'${} \times 10^{{{}}}$'.format(*'3.5e+20'.split('e+'))
Run Code Online (Sandbox Code Playgroud)
您可以执行以下操作:
ax.set_title( "${0} \\times 10^{{{1}}}$".format('3.5','+20'))
Run Code Online (Sandbox Code Playgroud)
在旧样式中:
ax.set_title( "$%s \\times 10^{%s}$" % ('3.5','+20'))
Run Code Online (Sandbox Code Playgroud)