在Matplotlib绘图中添加Latex风格公式

来源:互联网 发布:网络编辑发展前景 编辑:程序博客网 时间:2024/05/17 21:51

http://www.cnblogs.com/chaosimple/p/4031421.html


Matplotlib绘图的过程中,可以为各个轴的Label,图像的Title、Legend等元素添加Latex风格的公式。

只需要在Latex公式的文本前后各增加一个$符号,Matplotlib就可以自动进行解析,示例代码如下:

#!/usr/bin/env python#coding:utf-8"""  Author:  Chaos --<Chaosimpler@gmail.com>  Purpose: 在Matplotlib绘图过程中使用Latex风格的公式  Created: 2014年10月17日"""import matplotlib.pyplot as pltimport numpy as npif __name__ == '__main__':        x=np.arange(0.01,1,0.01)    y=0.5*np.log((1-x)/x)       plt.grid()        plt.subplots_adjust(top=0.9)    plt.scatter(x,y,label=r'$\alpha =\frac{1}{2}\ln(\frac{1-\varepsilon}{\varepsilon })$')        plt.legend()    plt.xlabel(r'$\varepsilon$',fontsize=20)    plt.ylabel(r'$\alpha$',fontsize=20)    plt.xlim(0,1)        plt.show()

该程序执行结果如下图所示:

b5838c2e-bc23-49eb-878e-a5e333f5545c


1 0
原创粉丝点击