How can I format a float using matplotlib's LaTeX formatter?

前端 未结 3 1466
南笙
南笙 2020-12-20 18:10

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?

3条回答
  •  没有蜡笔的小新
    2020-12-20 18:26

    Install the num2tex package:

    pip install num2tex
    

    and format your title as:

    ax.set_title('${}$'.format(num2tex(3.5e20)))
    

    or use the _repr_latex_() method:

    ax.set_title(num2tex(3.5e20)._repr_latex_())
    

    which will give you the same thing.

    num2tex inherits from str so the format function can be used as you would use it for a string:

    ax.set_title('${:.2e}$'.format(num2tex(3.5e20)))
    

    Disclaimer: I (very recently) created num2tex. It works well for my workflow and I am now trying to get feedback from others who might be interested in using it.

提交回复
热议问题