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


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 plt
import numpy as np


if __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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM