Baf*_*fel 5 python numpy matplotlib multidimensional-array point-clouds
我有一个2维Numpy NDarray填充0到8之间的浮点数.这个2维数组大小是(1000, 1600)
和大约1400个值(点云中的点),其余的值是None
,所以matplotlib不绘制这些值.您可以在下图中看到绘制的表格.我想要的是,使用旁边的值插值的None值具有渐变式热图.这个pointcloud代表屋顶的形状,我想把这个数据处理成一个我可以提供给神经网络以检测屋顶类型的图像.
我用于此图的代码很短,
import matplotlib.pyplot as plt
plt.clf()
#plotGrid is the numpy.ndarray with shape (1000, 1600) and dtype float
plt.imshow(plotGrid, cmap='gray', interpolation='nearest')
plt.colorbar()
plt.show()
Run Code Online (Sandbox Code Playgroud)
我认为scipy.interpolate.interp2d
可以满足您的需要:
import scipy.interpolate
z_all = plotGrid.astype(float) # convert nones to nan
x_all, y_all = np.indices(plotGrid.shape) # get x and y coordinates
# convert to 1d arrays of coordinates
valid = ~np.isnan(z_all)
x, y, z = x_all[valid], y_all[valid], z_all[valid]
# interpolate
interp = scipy.interpolate.interp2d(x, y, z)
filled_data = interp(x_all[:,0], y_all[0,:]) # this is kinda gross, but `interp` doesn't
# do normal broadcasting
plt.imshow(filled_data)
Run Code Online (Sandbox Code Playgroud)