已知空间坐标和坐标所对应的属性(高程,温度等),利用Python的numpy和matplotlib绘制等值线图,三维表面图

已知空间坐标和所对应的属性值,例如高程,温度等,绘制等值线图和三维图,网上的很多代码都是用规则的模拟数据,那么对于任意坐标的绘图代码如下。



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

if __name__ == '__main__':
    data = np.loadtxt("surface.txt", dtype=float)
    x, y, z = data[:, 0], data[:, 1], data[:, 2]

    # 准备待插值位置
    xi = np.linspace(x.min(), x.max(), 200)
    yi = np.linspace(y.min(), y.max(), 200)
    X, Y = np.meshgrid(xi, yi)

    # 插值
    Z = griddata((x, y), z, (X, Y), method='cubic')

    # 绘图
    fig = plt.figure()
    ax1 = fig.add_subplot(121, projection='3d')
    ax1.plot_surface(X, Y, Z, cmap='jet')
    ax1.contour(X, Y, Z, cmap='jet')

    ax2 = fig.add_subplot(122)
    ax2.contourf(X, Y, Z, cmap='jet')

    plt.tight_layout()
    plt.show()

 

surface.txt文件中格式如下(分别为x,y,z)

494612.94429657    3138019.81148885   270.00000000
494617.04198272    3138017.35584132   270.00000000
494608.67754848    3138021.96524233   270.00000000
494599.79420854    3138025.43586221   270.00000000
494604.33556867    3138023.96003760   270.00000000
494620.89079138    3138014.52485634   270.00000000
494637.60138951    3138005.62712341   270.00000000
494642.21095297    3138004.36722736   270.00000000
494633.05758053    3138007.09902805   270.00000000
494624.64968405    3138011.57281989   270.00000000
494628.69659326    3138009.03897110   270.00000000
494595.13268506    3138026.49132046   270.00000000

......

效果如图

已知空间坐标和坐标所对应的属性(高程,温度等),利用Python的numpy和matplotlib绘制等值线图,三维表面图_第1张图片

 

 

你可能感兴趣的:(已知空间坐标和坐标所对应的属性(高程,温度等),利用Python的numpy和matplotlib绘制等值线图,三维表面图)