在Matplotlib绘图中添加Latex风格公式
关键词:matplotlib latex、matplotlib 绘图、matplotlib 实时绘图
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()
该程序执行结果如下图所示:

