How to make a spatial plot
23 views (last 30 days)
Show older comments
Elliot Jenkins
on 10 Jan 2023
Commented: Walter Roberson
on 11 Jan 2023
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.
0 Comments
Accepted Answer
Walter Roberson
on 10 Jan 2023
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
Walter Roberson
on 11 Jan 2023
'Severity' is not a valid property name for imagesc . The property name is the one I gave in my code, 'AlphaData'
Walter Roberson
on 11 Jan 2023
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
More Answers (0)
See Also
Categories
Find more on Geographic Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!