Hi,
I have latitude, longtitude and the data corrosponding to each location. How do I make a simple spatial plot of the data on a map with a gradient scale? I have done all the information in excel and imported it into MATLAB so they are in tables.
I do not have data for every location for the country so also I would like to grey out the areas that I do not have the data for (so locations with 0 are not confused with areas with no data).
Thanks.

 Accepted Answer

You can discretize the coordinates, and accumarray, something like
dxlat = 0.1; %degree
dylon = 0.25; %degree
latidx = floor((lat-min(lat))/dxlat) + 1;
lonidx = floor((lat-min(long))/dxlon) + 1;
mean_data = accumarray([latidx(:), lonidx(:)], data(:), [], @mean, nan );
Now you can do gradient processing. (Locations with no data in the dxlat x dylon box will have nan in them, which is going to cause problems for the gradients.)

6 Comments

Hello,
Thank you for your answer. I am sorry but I do not understand your answer. I am not very good nor experienced at MATLAB so if you could try to explain the solution in the simplest terms it would be much appreciated. I will break the question down.
1) What function to use to create a spatial plot with coordinates and data.
2) I do not think the gradient scale would work for my problem now. But how could I assign different ranges of values with different colours on the map?
3) How to grey out everywhere else on the map that has no data
%assuming vector lat, vector lon, vector data
dxlat = 0.1; %degree
dxlon = 0.25; %degree
minlat = min(lat);
minlon = min(long);
latidx = floor((lat-minlat)/dxlat) + 1;
lonidx = floor((lat-minlon)/dxlon) + 1;
mean_data = accumarray([lonidx(:), latidx(:)], data(:), [], @mean, nan );
scaled_lat = ((1:size(mean_data,2))-1/2) * dxlat + minlat;
scaled_lon = ((1:size(mean_data,1))-1/2) * dxlon + minlon;
alphadata = double(~isnan(mean_data));
imagesc(mean_data, 'XData', scaled_lon, 'YData', scaled_lat, 'AlphaData', alphadata);
colorbar();
set(gca, 'color', [0.5 0.5 0.5]); %grey behind no data
Notes:
I arbitrarily decided to discretize by 0.1 degree latitude and 0.25 degree longitude, just to show that it was possible. You should decide the latitude and longitude pixel resolution that you want.
I decided that what you wanted would be the mean of all of the data that falls into a particular pixel -- rather than, for example, the sum. If the data happens to represent something like rain then possibly you want the sum instead of the mean.
Hi,
Thank you for the reply. I do not understand what is happening with the code but I have tried to run it and have found some errors. I replaced the parameters in your code with my parameters so the code looks like this. latitude70, longtitude70 and severity70 are all arrays converted from tables.
A= readtable("1970s.xlsx")
S= table2array(A)
latitude70= S(:,1);
longitude70= S(:,2);
severity70= S(:,4);
% data
%assuming vector lat, vector lon, vector data
dxlat = 0.1; %degree
dxlon = 0.25; %degree
minlat = min(latitude70);
minlon = min(longitude70);
latidx = floor((latitude70-minlat)/dxlat) + 1;
lonidx = floor((latitude70-minlon)/dxlon) + 1;
mean_data = accumarray([lonidx(:), latidx(:)], severity70(:), [], @mean, nan );
scaled_lat = ((1:size(mean_data,2))-1/2) * dxlat + minlat;
scaled_lon = ((1:size(mean_data,1))-1/2) * dxlon + minlon;
alphadata = double(~isnan(mean_data));
imagesc(mean_data, 'XData', scaled_lon, 'YData', scaled_lat, 'Severity', alphadata);
colorbar();
set(gca, 'color', [0.5 0.5 0.5]); %grey behind no data
I am getting two errors:
Error in imagesc (line 52)
hh = image(varargin{:}, 'CDataMapping', 'scaled');
Error in Maptests (line 35)
imagesc(mean_data, 'XData', scaled_lon, 'YData', scaled_lat, 'Severity', alphadata);
'Severity' is not a valid property name for imagesc . The property name is the one I gave in my code, 'AlphaData'
I create random data for demonstration purposes. If you just happen to be running Linux or MacOS then this code will operate in demonstration mode, but if you are using Windows then this code will read your file.
if ~isunix()
A = readtable("1970s.xlsx")
S = table2array(A)
else
%random data for illustration purposes
rng(12345);
N = 5000;
S = zeros(N,4);
S(:,1) = (rand(N,1) * 2 - 1) * 20;
S(:,2) = (rand(N,1) * 2 - 1) * 30;
S(:,4) = randn(N,1) / 2 + 0.25;
S(randi(N, floor(N/20), 1), 4) = nan;
end
%the code
S = rmmissing(S(:,[1 2 4])); %get rid of any nan input
latitude70 = S(:,1);
longitude70 = S(:,2);
severity70 = S(:,3);
% data
%assuming vector lat, vector lon, vector data
dxlat = 1; %degree
dxlon = 1.5; %degree
minlat = min(latitude70);
minlon = min(longitude70);
latidx = floor((latitude70-minlat)/dxlat) + 1;
lonidx = floor((longitude70-minlon)/dxlon) + 1;
mean_data = accumarray([lonidx(:), latidx(:)], severity70(:), [], @mean, nan );
scaled_lat = ((1:size(mean_data,2))-1/2) * dxlat + minlat;
scaled_lon = ((1:size(mean_data,1))-1/2) * dxlon + minlon;
alphadata = double(~isnan(mean_data));
imagesc(mean_data, 'XData', scaled_lon, 'YData', scaled_lat, 'AlphaData', alphadata);
colorbar();
set(gca, 'color', [0.5 0.5 0.5]); %grey behind no data

Sign in to comment.

More Answers (0)

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!