python如何实现matlab_Python实现matlab数据绘制
自從使用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 為結束行標記
總結
以上是生活随笔為你收集整理的python如何实现matlab_Python实现matlab数据绘制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 测试mysql数据库_Py
- 下一篇: java类成员初始化_简单了解Java类