繁体   English   中英

从2D数据生成热图

[英]Generate heat map from 2D data

我有一组X,Y数据点(来自遥感图像),并且在本教程中绘制了一个散点图,链接如下: 使用散点数据集在MatPlotLib中生成一个热图

但是,当我尝试绘制热图时,会发生错误:无法显示热图。

有没有一种方法可以在热图中显示二维数据,热图中的不同颜色代表像素的密度?

这是我的代码和结果:

import rasterio
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from scipy.ndimage.filters import gaussian_filter


def myplot(x, y, s, bins=1000):
    heatmap, xedges, yedges = np.histogram2d(x, y, bins=bins)
    heatmap = gaussian_filter(heatmap, sigma=s)

    extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
    return heatmap.T, extent


fig, axs = plt.subplots(1, 2)

dataset = rasterio.open('E:/Jupyter Notebook/LC81490312016259LGN00/LC8_subset_layerstacking.tif')
red_band = dataset.read(4)
NIR_band = dataset.read(5)

np.seterr(divide='ignore', invalid='ignore')
ndvi = (NIR_band.astype(float)-red_band.astype(float))/(NIR_band.astype(float)+red_band.astype(float))

ndvi_flat = np.ndarray.flatten(ndvi)
red_band_flat = np.ndarray.flatten(red_band)


x = ndvi_flat
y = red_band_flat

sigmas = [0, 16]

for ax, s in zip(axs.flatten(), sigmas):
    if s == 0:
        ax.plot(x, y, 'k.', markersize=0.1)
        #ax.set_aspect('equal')
        ax.set_title("Scatter plot")
        ax.set_xlabel('NDVI')
        ax.set_ylabel('Red Reflectance')
    else:
        img, extent = myplot(x, y, s)
        ax.imshow(img, origin='lower',cmap=cm.jet)

        ax.set_title("Smoothing with  $\sigma$ = %d" % s)
        ax.set_xlabel('NDVI')
        ax.set_ylabel('Red Reflectance')

plt.show()

左图是黑色散点图(无像素密度信息),右图是热图

左图是黑色散点图(无像素密度信息),右图是热图

我需要处理的代码和数据存储在GitHub中: https : //github.com/Flyinfish-gzh/remote-sensing-data-visualization

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM