Creating a smooth surface plot from topographic data using matplotlib

夙愿已清 提交于 2021-02-04 11:41:27

问题


I have a code that creates a 3d topographic surface from lat, lon and elev data.

I'm using ax.plot_surface, which creates a topographic surface that looks like this:

I would like to smooth the data to create a picture that looks more like this:

Is there a better way to smooth out the interpolation done by mesh grid?

my_data is sorted by [lat,lon,elev] size(912,3)

Code below

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


fig = plt.figure()
ax = Axes3D(fig)
my_data = np.genfromtxt('2014_0.01_v3_HDF5.txt', delimiter = ',',     skip_header = 1)
my_data[my_data==0] = np.nan 
my_data = my_data[~np.isnan(my_data).any(axis=1)]
X = my_data[:,0] 
Y = my_data[:,1]
Z = my_data[:,2]
xi = np.linspace(X.min(),X.max(),(len(Z)/3))
yi = np.linspace(Y.min(),Y.max(),(len(Z)/3))
zi = griddata((X, Y), Z, (xi[None,:], yi[:,None]), method='nearest')

xig, yig = np.meshgrid(xi, yi)

surf = ax.plot_surface(xig, yig, zi, cmap='gist_earth')
fig.colorbar(surf, shrink=0.5, aspect=5)
ax.set_title('2014 ATM Data 0.01 Degree Spacing')
ax.set_xlabel('Latitude')
ax.set_ylabel('Longitude')
ax.set_zlabel('Elevation (m)')
ax.set_zlim3d(0,8000)

回答1:


You can replace the method of interpolation from nearest to cubic. It gives you a far better surface.

zi = griddata((X, Y), Z, (xi[None,:], yi[:,None]), method='cubic')


来源:https://stackoverflow.com/questions/33287620/creating-a-smooth-surface-plot-from-topographic-data-using-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!