python 讀取txt文件中的數據,刪除異樣數據,並畫圖

注意一下幾點

  • 使用matplotlib在的pyplot包畫圖
  • 使用open打開文件,使用readline()分行讀取
  • 使用numpy.delete(arrayname,index)刪除確定位置處的數據
  • 使用and和or而不是&&和||
  • 一個figure上畫多條線不用hold on, 且線色自動變化

代碼如下:

import matplotlib.pyplot as plt
import numpy as ny


def draw_posexy(filepath):
    pose_xy = []
    pose_theta = []
    pose = []
    with open(filepath, 'r') as f:
        while True:
            line = f.readline()
            if not line:
                break
            temp = line.strip('\n').split(',')
            temp = list(map(lambda x: float(x), temp))
            pose.append(temp)
    pose_x = ny.array([i[1] for i in pose])
    pose_y = ny.array([i[2] for i in pose])
    pose_theta = [i[3] for i in pose]

    index = []
    for i in range(len(pose)):
        if int(abs(pose_x[i])) > 1000 or int(abs(pose_y[i])) > 1000:
            #print(i)
            index.append(i)
        elif i > 0 :
            #if int(abs(pose_x[i] - pose_x[i - 1])) > 5 or int(abs(pose_y[i] - pose_y[i - 1])) > 5:
                #print("jap:" + i.__str__())
                #index.append(i)
            if (int(pose_x[i]) < -80 and int(pose_x[i-2]) > -40) or (int(pose_x[i - 2]) < -80 and int(pose_x[i]) > -40):
                print("middle ++++++++++++++" + i.__str__() + ": " + float(pose_x[i]).__str__() + "," + float(pose_y[i]).__str__())
    #print(index)
    for i in range(len(index)):
        print(index[i].__str__() + ":")
        print(float(pose_x[index[i]]).__str__() + "," + float(pose_y[index[i]]).__str__())
    pose_x = ny.delete(pose_x, index)
    pose_y = ny.delete(pose_y, index)
    return pose_x, pose_y, pose_theta

filepath = "/home/efan/MyCode/data/slipTest_data/2020-01-19_15-40-40/text_pose_out_noimu.txt"
filepath_1 = "/home/efan/MyCode/data/slipTest_data/2020-01-19_15-40-40/text_pose_out_imu.txt"
[pos_x, pos_y, pos_theta] = draw_posexy(filepath)
[pos_x_1, pos_y_1, pos_theta_1] = draw_posexy(filepath_1)
#
plt.plot(pos_x_1, pos_y_1, '--')
plt.plot(pos_x, pos_y)
plt.show()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章