Clear Filters
Clear Filters

How to create a heat map with tiles?

15 views (last 30 days)
Briana Bellomo
Briana Bellomo on 9 Mar 2022
Answered: Ayush on 12 Oct 2023
Hello, I am trying to create a heatmap with lat and long. I have a data set with sets of points (lat,lon). I would like a write a code for a heatmap that involves the colours or values as the frequency of points that occur within that tile.
For example, if my heatmap grid looks like the one here:
I would like for the colours or the value of each colour to be determined by how many points fall into that tile.
I've just plotted my lat and lon from my dataset with geodensityplot and geoscatter, but the tiles overlap each other. This plot is given by a code from geoscatter.
Another note is that some points in my dataset are repeated (lat and lon are repeated).
clear all;
T = readtable(['Project1.xlsx']);
Lat = T.lat;
Lon = T.long;
%remove NaN
Lat = Lat(~isnan(Lat))
Lon = Lon(~isnan(Lon))
%make a density heatmap
geodensityplot(Lat,Lon,'Radius',7000);
%counting how many a lat appears
C = [Lat,Lon]
[ii,jj,kk]=unique(C,'rows')
out=[ii histc(kk,1:size(ii,1))]
%geoscatter
figure
geoscatter(Lat,Lon);
A = 10*10;
C=out(:,3)
h = geoscatter(out(:,1),out(:,2),A,C,'filled','Marker','s','MarkerFaceAlpha',.4);
title("Number of Reports per Lat/Lon")
geobasemap colorterrain
%now I want to create a heatmap where the tiles do not overlap
%the tiles used will be 0.18 by 0.25
Lat_tile = 54.82 + 0.18 * [19 17 15 13 11 9 7 5 3 1]'
Lon_tile = -123.25 + 0.25 * [1 3 5 7 9 11]'
X = ones(454,1);
valuesMatrix = [X, Lat, Lon];
heatmap(Lon_tile,Lat_tile,valuesMatrix)

Answers (1)

Ayush
Ayush on 12 Oct 2023
Hi Briana,
I understand that you are trying to create a heat map to show the frequency of coordinate points that occurred in a tile.
To do so, you can follow the given steps:
  1. Initialize a matrix of zeros with dimensions equal to the number of latitude tiles and the number of longitude tiles using:
valuesMatrix = zeros(numel(Lat_tile), numel(Lon_tile));
This matrix will store the count of data points falling within each tile.
2. Create a nested for loop, that iterates over each latitude(i) and longitude(j) tile:
a. Create a logical index tileIndices that identifies the data points falling within the current tile in this manner:
tileIndices = Lat >= tileLat & Lat < tileLat + 0.18 & Lon >= tileLon & Lon < tileLon + 0.25;
b. Calculates the sum of tileIndices (number of data points falling within the current tile) and assigns it to the corresponding position in valuesMatrix.
valuesMatrix(i, j) = sum(tileIndices);
3. Plot the heatmap using:
heatmap(Lon_tile, Lat_tile, valuesMatrix);
Here Lon_tile and Lat_tile represent the longitude and latitude ranges of the tiles, respectively, and "valuesMatrix contains the count of data points falling within each tile. The resulting heatmap will show the density of data points in each tile without overlapping as seen in the image.
You can modify your code by referring to this:
% Create a heatmap without overlapping tiles
Lat_tile = 54.82 + 0.18 * (19:-2:1)';
Lon_tile = -123.25 + 0.25 * (1:2:11)';
% Calculate the values matrix for heatmap
valuesMatrix = zeros(numel(Lat_tile), numel(Lon_tile));
for i = 1:numel(Lat_tile)
for j = 1:numel(Lon_tile)
tileLat = Lat_tile(i);
tileLon = Lon_tile(j);
tileIndices = Lat >= tileLat & Lat < tileLat + 0.18 & Lon >= tileLon & Lon < tileLon + 0.25;
valuesMatrix(i, j) = sum(tileIndices);
end
end
% Plot the heatmap
figure
heatmap(Lon_tile, Lat_tile, valuesMatrix);
title('Heatmap of Reports per Tile');
xlabel('Longitude');
ylabel('Latitude');
For more information on the “heatmap” function, please refer to the following link:
Hope this helps!
Regards,
Ayush.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!