How to fill polygons based on attribute values?

I have used the following code to load and plot the shapefile "Expot.shp" (attaced zip file). I wanted to plot the "Corr" attribute, but for some reason the colour does not match the values. (Also, the "Corr" values with zeros are basically not calculated - can treat them as NaNs)
landAreas = readgeotable("landareas.shp"); %from "geoshow" matlab documentation
row = landAreas.Name == "North and South America";
landAreasSubset = landAreas(row,:);
corr = shaperead("Expot.shp")
cmap = jet(256);
colorRange = makesymbolspec("Polygon", ...
{'Corr',[-0.02 0.05],'FaceColor',cmap}); %Use makesymbolspec to specify polygon colors
mapshow(corr,"SymbolSpec",colorRange)
colormap(cmap)
caxis([-0.02 0.05])
colorbar
hold on
geoshow(landAreasSubset)
The result map is attached below
The legend shows the "Corr" values but the map does not. I'd like to know where I seem to be going wrong; I think there's a line of code missing or needs to be changed.
I'd be grateful for assistance on this. Thank You

 채택된 답변

KSSV
KSSV 2022년 6월 23일
편집: KSSV 2022년 6월 23일

0 개 추천

How about this?
shpFile = 'Expot.shp' ;
S = shaperead(shpFile) ;
N = length(S) ;
cmap = jet(N) ;
figure
hold on
for i = 1:N
x = S(i).X ; y = S(i).Y ;
x(isnan(x))=[] ;
y(isnan(y))=[] ;
patch(x,y,cmap(i,:)) ;
end
colorbar
colormap(cmap)

댓글 수: 11

Thank you for your reply @KSSV
I don't think the "Corr" values are refelcted in the polygons. Because most of the polygons don't have values in them (NaNs, but they got converted to zeros here). (For context, I have attached an excel file with just the "Corr" values and a map showing the regions)
So basically, the regions in the lower half have no values, but from the Matlab code, it seems to show values above 0.5.
Out of curiosity, does the "length(S)" take in the "Corr" values? Because each polygon has a specific "Corr value".
KSSV
KSSV 2022년 6월 23일
length(S) is the number of polygons present in the shape file.
Keegan Carvalho
Keegan Carvalho 2022년 6월 23일
편집: Keegan Carvalho 2022년 6월 23일
Actually, some of the "Corr" values are negative, so the above plot does not reflect those values. I was trying to find the issue in my code, because the legend is correct, but the polygons do not show those colours.
Is there another way of going about this issue?
KSSV
KSSV 2022년 6월 23일
Hey there is a correction in the code, given, I forgot to update the colormap. Is the present corrected plot is fine?
Keegan Carvalho
Keegan Carvalho 2022년 6월 23일
편집: Keegan Carvalho 2022년 6월 23일
Unfortunately not. There seems to be a problem with the values. The polygons are basically the "Divisions" and each Division has a corresponsing "Corr" value.
You are right....Try this after the plot.
C = [S.corr] ;
caxis([min(C) max(C)])
Keegan Carvalho
Keegan Carvalho 2022년 6월 23일
편집: Keegan Carvalho 2022년 6월 23일
This doesn't change the colour of the polygons. Only the colorbar gets updated.
Is there something in the loop that needs to be changed? I still can't seem to understand why the colorbar shows the values but the polygons don't.
KSSV
KSSV 2022년 6월 23일
I am picking the colors from cmap....the colorbar and polygon colors will match.
Keegan Carvalho
Keegan Carvalho 2022년 6월 23일
편집: Keegan Carvalho 2022년 6월 23일
I understand; the thing is, most of the areas that are RED in the map actually have 0 (or NaN) as their "Corr" values. So I'm not sure where the issue lies.
Also, I wanted to plot the world map in the background as the image I posted above; but within the confines of the shapefile (and not the entire Americas). Could you please assist me in that?
KSSV
KSSV 2022년 6월 23일
This is the correct mapping of corr right?
Keegan Carvalho
Keegan Carvalho 2022년 6월 24일
편집: Keegan Carvalho 2022년 6월 24일
Not quite. The regions with values "0", I prefer not having a colour for them, becasue they are essentially "NaN" values. Is there a way to code to show no colour for the polygons with "0" values?
If the regions with "0" show no color, then I can determine if it is correct. Because most of the polygons with "0" show different colours in the above map

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Color and Styling에 대해 자세히 알아보기

질문:

2022년 6월 23일

편집:

2022년 6월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by