[英]Plot a surface with lists of latitude, longitude and elevation data (hillshading)
我在 matplotlib 網站上找到了這個腳本:
"""
Demonstrates using custom hillshading in a 3D surface plot.
"""
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cbook
from matplotlib import cm
from matplotlib.colors import LightSource
import matplotlib.pyplot as plt
import numpy as np
filename = cbook.get_sample_data('jacksboro_fault_dem.npz', asfileobj=False)
with np.load(filename) as dem:
z = dem['elevation']
nrows, ncols = z.shape
x = np.linspace(dem['xmin'], dem['xmax'], ncols)
y = np.linspace(dem['ymin'], dem['ymax'], nrows)
x, y = np.meshgrid(x, y)
region = np.s_[5:50, 5:50]
x, y, z = x[region], y[region], z[region]
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
ls = LightSource(270, 45)
# To use a custom hillshading mode, override the built-in shading and pass
# in the rgb colors of the shaded surface calculated from "shade".
rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode='soft')
surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,
linewidth=0, antialiased=False, shade=False)
plt.show()
他們使用文件jacksboro_fault_dem.npz
來繪制高程數據,他們得到如下內容:
多虧了谷歌地球,我才能獲得包含以下區域(留尼汪島馬伊多)的緯度、經度和海拔數據的文本文件maido_elevation_data.txt
:
我做了一個函數來從文本文件中為每個坐標獲取 3 個列表:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
def get_LAT_LONG_ALT(text_file):
ch=""
LAT=[]
LONG=[]
ALT=[]
with open(text_file,"r") as fich:
for ligne in fich:
for e in ligne:
ch+=e
liste=ch.replace("\n","").split("\t")
LAT.append(float(liste[0]))
LONG.append(float(liste[1]))
ALT.append(float(liste[2]))
ch=""
return LAT,LONG,ALT
fig = plt.figure()
axes = fig.add_subplot(111, projection="3d")
X = get_LAT_LONG_ALT("maido_elevation_data.txt")[0]
Y = get_LAT_LONG_ALT("maido_elevation_data.txt")[1]
Z = get_LAT_LONG_ALT("maido_elevation_data.txt")[2]
axes.scatter(X,Y,Z, c="r", marker="o")
axes.set_xlabel("Latitude")
axes.set_ylabel("Longitude")
axes.set_zlabel("Altitude")
plt.show()
我應該如何修改腳本以像他們一樣使用我自己的數據獲得良好的曲面圖?
PS:我會在評論中給你文件的鏈接,因為我不允許放超過 2 個鏈接......是的,我是新的 :)
你應該重塑你的數據,它是一個三列數據 x、y 和 z 你應該有一個只有 z 值的文件在 2D 表中的列是 x,行是 y。 python 中的 Meshgrid 功能應該會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.