Python 使用matplotlib绘制DEM的速度太慢
我用matplotlib、mplot3d和gdal制作了一个曲面图。代码如下:Python 使用matplotlib绘制DEM的速度太慢,python,matplotlib,gdal,geotiff,Python,Matplotlib,Gdal,Geotiff,我用matplotlib、mplot3d和gdal制作了一个曲面图。代码如下: import gdal from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm import matplotlib.pyplot as plt import numpy as np # maido is the name of a mountain # tipe is the name of a french school project
import gdal
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np
# maido is the name of a mountain
# tipe is the name of a french school project
# 1) opening maido geotiff as an array
maido = gdal.Open('dem_maido_tipe.tif')
dem_maido = maido.ReadAsArray()
# 2) transformation of coordinates
columns = maido.RasterXSize
rows = maido.RasterYSize
gt = maido.GetGeoTransform()
x = (columns * gt[1]) + gt[0]
y = (rows * gt[5]) + gt[3]
X = np.arange(gt[0], x, gt[1])
Y = np.arange(gt[3], y, gt[5])
# 3) creation of a simple grid without interpolation
X, Y = np.meshgrid(X, Y)
# 4) deleting the "no data" values
# delete the last column
dem_maido = np.delete(dem_maido, len(dem_maido)-1, axis = 0)
X = np.delete(X, len(X)-1, axis = 0)
Y = np.delete(Y, len(Y)-1, axis = 0)
# delete the last row
dem_maido = np.delete(dem_maido, len(dem_maido[0])-1, axis = 1)
X = np.delete(X, len(X[0])-1, axis = 1)
Y = np.delete(Y, len(Y[0])-1, axis = 1)
# 5) plot the raster
fig, axes = plt.subplots(subplot_kw={'projection': '3d'})
surf = axes.plot_surface(X, Y, dem_maido, rstride=1, cstride=1, cmap=cm.gist_earth,linewidth=0, antialiased=False)
plt.colorbar(surf) # adding the colobar on the right
plt.show()
这是我使用的GeoTiff文件(它上传到我自己的google帐户上,不用担心):
下面是我得到的:
我对结果很满意,但当我尝试移动它时,它需要很多时间
我能做些什么使它更快呢?大胆猜测:你有一个大约600x1000个细胞的表面。我不确定mplot3d查看器的优化程度(但我有一些疑问),但可能太多了。尝试重新采样您的数据。或者查看更高级的三维可视化工具包。还有,您使用的是哪台机器?对于这么多的数据,我发现即使我尝试使matplotlib变慢,也永远不够,所以现在我使用的是mayavi,它可以工作:)