您的位置:首页 > 编程语言 > PHP开发

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

2016-04-24 17:22 651 查看
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 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()


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



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: