【原】在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