0

I have a set of X,Y data points(from Remote sensing image),and I drew a scatter plot through the tutorial, the link is as follows:Generate a heatmap in MatPlotLib using a scatter data set

But when I try to draw a heat map, an error occurs:Heatmap cannot be displayed.

Is there a way to display two-dimensional data in a heat map, the different colors in the heat map represent the density of the pixels?

This is my code and result:

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()

The left image is a black scatter plot (no pixel density information), and the right image is a heat map

The left image is a black scatter plot (no pixel density information), and the right image is a heat map

The code and the data I need to process are stored in GitHub:https://github.com/Flyinfish-gzh/remote-sensing-data-visualization

Flyinfish
  • 1
  • 2
  • What is the exact error message you receive? I tried to implement your code using random input data and it works just fine for me. Also please notice that rainbow colormaps are evil: https://bids.github.io/colormap/ It's better to use a perceptually-uniform colormap like viridis instead. – Solvalou Nov 30 '19 at 15:59
  • @Solvalou There is no error message, but the heat map cannot be displayed correctly. When using random data at the time, the code worked correctly, but when using the data I needed to process, the heatmap could not be displayed accurately. I tried to check the difference between random data and the data I need to process, but I didn't find any difference except the data size. – Flyinfish Dec 01 '19 at 03:24
  • Okay, now I'm a bit confused about what really is your problem. So the image you attached is the result you got, right? The scatter plot on the left-hand side looks like you expect it, but the heatmap on the right-hand side doesn't? First of all, you didn't use the `extent` variable returned by `myplot` in `imshow`. Otherwise, the output looks fine to me. Maybe there just is an issue with the data, as there seems to be a location which is really dense (the red dot in the image). – Solvalou Dec 01 '19 at 13:03
  • You could try it with a log-scale. Just import `LogNorm` via `from matplotlib.colors import LogNorm` and use it in `imshow` via `ax.imshow(img, origin='lower', norm=LogNorm(vmin=np.min(img[img > 0]), vmax=np.max(img)))`. Here, `vmin` and `vmax` are extreme values of your histogram. – Solvalou Dec 01 '19 at 13:07
  • @Solvalou Thank you very much for your suggestion to solve some of my problems. I intentionally deleted 'extent' variable because only in this way can the image on the right be displayed.Can I send you the data file for you to check? Because it's hard to describe these problems clearly. – Flyinfish Dec 01 '19 at 14:34
  • Sure, I can take a look at it. You could upload your data somewhere and edit the link into your post. – Solvalou Dec 01 '19 at 16:09
  • If you want a graphical representation of pixel density you can have it directly in the scatter plot, using transparency. You may have to experiment a little with the transparency value... – gboffi Dec 01 '19 at 16:39
  • @ Solvalou I have uploaded the code and data to github and the link is at the bottom of my post. If you have time, please help me take a look. – Flyinfish Dec 02 '19 at 03:01

0 Answers0