Python读取xyz文件并绘制点云图

先导入所需的库

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

定义xyz文件读取函数,返回一个np数组

def read_xyz(filename):
    locs = []
    with open(filename, 'r') as f:
        l = f.readline()  # header line
        while l:
            l = f.readline().strip().split(";")
            if (l != ['']):
                locs.append([float(l[0]), float(l[1]), float(l[2])])
            else:
                l = None
    return np.array(locs)

调用plt库

array = read_xyz('1.XYZ')
x = array[:,0]
y = array[:,1]
z = array[:,2]

fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(x,y,z,c=np.sqrt(x ** 2 + y ** 2 + z ** 2),cmap="jet", marker="o")
plt.show()

其中scatter函数的参数可以参考链接
Python读取xyz文件并绘制点云图_第1张图片

你可能感兴趣的:(python,python,开发语言)