自从使用python脚本后,经常用来调试数据的matlab终于有了一个简单易用的替代方案,相比matlab的.m语言,我更喜欢用python来解决。毕竟在python当中,相比.m主意很多编程工作都可以轻而易举地完成。
首先,先亮图,这效果不比matlab差,是吧?
绘制曲线
一、例程
1、测试环境
import matplotlib.pyplot as plt
上面代码不能正常使用时,说明并未安装模块需要执行
pip install matplotlib
2、要显示的数据
以下代码,来自于项目的工具代码,主要用来绘制log中的数据是否异常,因此直接使用log的输出数据:
11-18 20:45:15.540 23084-23150/hud.haliai.com.testarway E/HALO+testMovePath: python origin path start
11-18 20:45:15.542 23084-23150/hud.haliai.com.testarway E/HALO+testMovePath: python 1.0 , 0.0 ,0.0
11-18 20:45:15.543 23084-23150/hud.haliai.com.testarway E/HALO+testMovePath: python 1.0 , 1.0 ,0.0
11-18 20:45:15.543 23084-23150/hud.haliai.com.testarway E/HALO+testMovePath: python 1.2 , 2.0 ,0.0
11-18 20:45:15.544 23084-23150/hud.haliai.com.testarway E/HALO+testMovePath: python 1.5 , 2.2 ,0.0
11-18 20:45:15.544 23084-23150/hud.haliai.com.testarway E/HALO+testMovePath: python 2.5 , 2.0 ,0.0
11-18 20:45:15.544 23084-23150/hud.haliai.com.testarway E/HALO+testMovePath: python 3.0 , 2.5 ,0.0
11-18 20:45:15.544 23084-23150/hud.haliai.com.testarway E/HALO+testMovePath: python 3.3 , 3.0 ,0.0
3、python 实现代码
import matplotlib.pyplot as plt
import os
import sys
import operator
def getFileVect3(path,filterTag=' ',startTag='start',endTag='end'):
print [path,filterTag,startTag,endTag]
lineFile = open(path).read().split('\n')
print 'lineFile type is '+str(type(lineFile))
oriStart = 0
oriEnd = len(lineFile)
number = 0
for line in lineFile:
if oriStart==0 and line.find(startTag)!=-1:
oriStart=number
elif line.find(endTag)!=-1:
oriEnd=number
number+=1
oriPathX = []
oriPathY = []
oriPathZ = []
indexs = []
for i in range(oriStart+1,oriEnd):
raw=lineFile[i]
# print 'raw type is '+str(type(raw))
if operator.contains(raw,filterTag):
line=raw.split(filterTag)
if len(line) != 2:
print 'line filter tag error !'
head,content = line
vect3=content.strip().split(',')
if len(vect3) == 3:
strX,strY,strZ = vect3
oriPathX.append(float(strX))
oriPathY.append(float(strY))
oriPathZ.append(float(strZ))
indexs.append(float(i))
# print strX,strY,strZ
return indexs,oriPathX,oriPathY,oriPathZ
# python draw_line.py data.txt split s_tag e_tag
if len(sys.argv) != 5:
print 'bad para!'
exit()
cmd,filename,split_tag,start_tag,end_tag=sys.argv
print [filename,split_tag,start_tag,end_tag]
i,x,y,z,=getFileVect3('./'+filename,split_tag,start_tag,end_tag)
plt.plot(x, y, 'r', label='line 1', linewidth=1.5)
plt.ylabel('Test ARWay line convert!')
plt.axis('equal')
plt.show()
4、使用方式
将真保存于文件中,输入指令,满足的格式如下:
python draw_line.py data.txt python start end
其中
data.txt 为数据文件
python 为split拆分tag的keyword
start 为起始行标记
end 为结束行标记