如何使用matplotlib的LaTeX格式化程序格式化浮点数? [英] How can I format a float using matplotlib's LaTeX formatter?

查看:209
本文介绍了如何使用matplotlib的LaTeX格式化程序格式化浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python脚本中有一个数字,希望用作matplotlib中图形标题的一部分.是否有将浮点数转换为格式化的TeX字符串的函数?

I have a number in my python script that I want to use as part of the title of a plot in matplotlib. Is there a function that converts a float to a formatted TeX string?

基本上,

str(myFloat)

返回

3.5e+20

但我想要

$3.5 \times 10^{20}$

或至少让matplotlib格式化浮点格式,就像格式化第二个字符串一样.我也一直使用python 2.4,因此特别喜欢在旧版本中运行的代码.

or at least for matplotlib to format the float like the second string would be formatted. I'm also stuck using python 2.4, so code that runs in old versions is especially appreciated.

推荐答案

您可以执行以下操作:

ax.set_title( "${0} \\times 10^{{{1}}}$".format('3.5','+20'))

以旧样式:

ax.set_title( "$%s \\times 10^{%s}$" % ('3.5','+20'))

这篇关于如何使用matplotlib的LaTeX格式化程序格式化浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆