python 實現讀取txt 並畫三維圖

python 實現讀取txt 並畫三維圖

基於Anaconda spyder運行

直接上代碼:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
#自己編寫的讀取txt內容的函數 
def read_txt(path):
    infile = open(path,'rb')
    data = infile.read().decode('utf-8')
    print("Success for reading :\t%s" %path)#成功的話給出提示
    return data

def main():
    

    path="C:/Users/yiqing/Desktop/變形監測點數據.txt"
    data2=read_txt(path)
    idata = data2.split("\r\n")
    print(idata)#首先先進行spilt分割
    
    sum_points=int(idata[0])
    list_x=[]
    list_y=[]
    list_z=[]
    
 
    
    for i in range(sum_points):
        temp=idata[i+1].split(",")
        list_x.append(float(temp[0]))
        list_y.append(float(temp[1]))
        list_z.append(float(temp[2]))
        
    ax = plt.figure().add_subplot(111, projection = '3d')    
    ax.set_xlabel('X Label')
    ax.set_zlabel('Y Label')
    ax.set_zlabel('Z Label')
    ax.scatter(list_x, list_y, list_z, c = 'b', marker = '^') #點爲紅色三角形
  
    
main()

讀取的原文件:

裝在目錄: C:/Users/yiqing/Desktop/變形監測點數據.txt

24
32.5801,-52.7876,0.1369
32.5786,-52.7892,0.1380
32.5784,-52.7845,0.1411
32.5812,-52.7852,0.1393
32.5782,-52.7863,0.1394
32.5791,-52.7852,0.1354
32.5788,-52.7841,0.1414
32.5788,-52.7817,0.1375
32.5745,-52.7833,0.1359
32.5815,-52.7854,0.1327
32.5822,-52.7841,0.1358
32.5839,-52.7826,0.1361
32.5820,-52.7852,0.1339
32.5800,-52.7863,0.1325
32.5792,-52.7845,0.1416
32.5807,-52.7834,0.1395
32.5778,-52.7846,0.1412
32.5792,-52.7843,0.1371
32.5794,-52.7833,0.1406
32.5806,-52.7841,0.1411
32.5800,-52.7863,0.1380
32.5785,-52.7840,0.1368
32.5811,-52.7848,0.1412
32.5828,-52.7863,0.1356

輸出的三維散點圖:

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章