How to make a spatial plot
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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.
채택된 답변
Walter Roberson
2023년 1월 10일
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
Elliot Jenkins
2023년 1월 10일
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
Walter Roberson
2023년 1월 10일
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);
Walter Roberson
2023년 1월 11일
'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

추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
태그
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
