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

 Accepted Answer

KSSV
KSSV on 23 Jun 2022
Edited: KSSV on 23 Jun 2022
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 Comments

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".
length(S) is the number of polygons present in the shape file.
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?
Hey there is a correction in the code, given, I forgot to update the colormap. Is the present corrected plot is fine?
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)])
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.
I am picking the colors from cmap....the colorbar and polygon colors will match.
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?
This is the correct mapping of corr right?
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

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!