我试图绘制两个tiffs的高程差。出现以下错误消息:TypeError:图像数据的无效形状(1,2355,2676)。它引用im = ax. imshow.....行。我不太了解此问题或该怎么做。感谢您的帮助!
# import relevant moduls
from osgeo import gdal
import numpy as np
import matplotlib.pyplot as plt
from pygeotools.lib import iolib, warplib, geolib, timelib, malib
# Specify input files
dem_old = '../data/2007.tif'
dem_new = '../data/2018.tif'
# generate a list of the files
dem_fn_list = [dem_old, dem_new]
# combine dems with warplib and set to extent of intersection
ds_list = warplib.memwarp_multi_fn(dem_fn_list, extent='intersection', res='min', t_srs=dem_new)
# Load datasets to NumPy masked arrays
dem_old, dem_new = [iolib.ds_getma(i) for i in ds_list]
# Calculate elevation difference for each time period
dh_list = [dem_new - dem_old]
# plot elevation difference
print('Plotting DEM difference...')
plt.rcParams.update({'font.size': 15})
fig = plt.figure(figsize=(12, 12))
ax = fig.add_subplot(1,1,1)
im = ax.imshow(dh_list, origin='upper', interpolation='None', cmap='seismic_r', vmin = -40, vmax = 40)
# add colorbar
fig.colorbar(im)
# add title
plt.title("DSM elevation differences")
# disable xticks
plt.xticks([])
plt.yticks([])
# save figure
plt.savefig('glacier_change.png')
# show figure
plt.show()
1条答案
按热度按时间q1qsirdb1#
当你绘制“图像”(
dh_list
)时,该数组的维数为(1, 2355, 2676)
,然后它被解释为一个高度为1,宽度为2355,通道数为2676(!)的图像。Matplotlib不知道如何处理这个问题,并抛出一个错误。按照their documentation中的规定
支持的阵列形状包括:
前两个维度(M,N)定义图像的行和列。
因此,您需要重塑图像,在您的情况下,将
dh_list
定义为