In MATLAB, I want to plot a map with a legend showing different regions. - matlab

In MATLAB, I want to plot a map with a legend showing different regions.
For example:
my_map = shaperead('my_shape_file.shp');
Equator = [0.1 0.5 0.8];
Tropical = [0.8 0.4 0.6];
Subtropical = [0.7 0.1 0.5];
mapColors = makesymbolspec('Polygon',{'GRID_CODE',1,'Facecolor',Equator},{'GRID_CODE',2,'Facecolor',Tropical},{'GRID_CODE',3,'Facecolor',Subtropical});
h = mapshow(my_map,'SymbolSpec',mapColors);
legend(h,{'Equatorial','Tropical','Subtropical'})
axis off
However, this legend doesn't work - it only shows 'Equatorial' region. Any suggestions?
Additional query: I also want to show city names on the map. How do I (a) find out whether my shapefile contains city names/labels and (b) show these on the map?
Thank you!

Related

MATLAB-Patch between prediction bounds?

I have some data fitted to an exponential function (y1) using the curve fitting app to calculate the coefficients. I then imported the model and the prediction intervals (p11; a 1441x2 matrix). (t and Asiii are imported datasets, just generated some data to explain the code)
S=[0.95,0.87,0.83,0.73,0.62,0.51,0.41,0.31,0.22,0.04,0.002,0,0,0,0];
t=[1,3,5,10,20,30,40,50,60,90,120,180,240,360,1440];
hold on
s2=scatter(t,Asiii,250,[0.1216, 0.8, 0.0157],'^','filled');
x = 0:1440;
y1 = 1.016*exp(-0.02349*x)
p1=plot(x,y1,'Color',[0.1216, 0.8, 0.0157],...
'LineWidth',1.5);
p11 = predint(model,x,0.95,'observation','on');
plot(x,p11,'--','Color',[0.4235, 0.9608, .6],...
'LineWidth',1.5);
%Patch
patch([x;x],[p11(:,1)';p11(:,2)'],'g');
%Fill
fill(x, p11,p11,'facecolor', 'red', 'edgecolor', 'none', 'facealpha', 0.4);
I want to have the area between the prediciton bounds/confidence intervals filled with a different color, but it seems I can only get a solid black shading using 'patch'.
I also tried using the 'fill' function but the polygons go all the way to the top of y-axis. Is there a way to adjust the Edge thickness/color?

Add 0.5x0.5 degree grid on drawing of country outline

How do I draw a 0.5 degree x 0.5 degree grid over the country map in a MATLAB figure?
The code below gives me a gridded figure but not with 0.5x0.5 degree spacing.
borders('Iran Islamic Republic of')
grid on
ax.GridLineStyle = '-';
Can anyone tell me how to add 0.5x0.5 grid along x and y-axis to this figure?
The borders function is taken from the MATLAB File Exchange
You can use xticks() and yticks() functions (matlab tutorial). Your code should be something like:
borders('Iran Islamic Republic of')
grid on
ax.GridLineStyle = '-';
% Modify the X and Y ticks positions
xticks([44:.5:65]);
yticks([25:.5:40]);
This creates ticks every 0.5 degrees (from degree 44 until 65 in x, and from 25 to 40 in y). If the tick labels are overlaping, you can delete some. For example for the x-axis:
%Delete some labels, otherwise overcrowded
xlabels = xticklabels();
for i=2:2:length(xticks())
xlabels(i)={''};
end
xticklabels(xlabels)

How to change radial tick markers in Matlab polar plot?

I'd like to relabel the radial tick markers in the following polar log plot:
f = figure ;
t = 0:0.01: pi/2 ;
polar(t, 10 * log10(cos(t))/(50) + 1)
from 1, 0.8, 0.6, 0.4, 0.2 to 0, -10, -20, -30, -40 (i.e. radial dB ticks).
Trying some of the methods from Fixing the Radial Axis on MATLAB Polar Plots, I was able to relabel the markers provided my tick markers were positive and increasing.
I tried the following based on How to remove Rho labels from Matlab polar plot?
rho_labels = {'1' '0.8' '0.6' '0.4' '0.2'};
rho_labels2 = {'0' '-10' '-20' '-30' '-40'};
for r=1:length(rho_labels)
ff = findall(f, 'string', rho_labels{r}) ;
ff = rho_labels2{r} ;
end
but it also didn't work (seems to do nothing, so I suspect I'm operating on a copy of the find results not handled).
How can these tick markers be adjusted? Also, if I wanted a different number of concentric circles than 5, how can that be done (for example, 4 subdivisions with -40 dB at the "origin".)
Here is a way to rename the radial tick labels. Be warned that if there is a match between any radial and angular tick mark labels, both will be replaced and the angular labels will be wrong. But the angular tick labels are fixed as 0, 30, 60, ..., 330, so as long as the radial tick labels don't include these values, you should be fine.
What the code does is find all the text in the plot using findall, trim the blank spaces at the front of each string, then, for each entry in rho_labels, set the string entry corresponding to the tick label with that identifier to the corresponding entry in rho_labels2.
f = figure ;
t = 0:0.01: pi/2 ;
polar(t, 10 * log10(cos(t))/(50) + 1)
rho_labels = {'1' '0.8' '0.6' '0.4' '0.2'};
rho_labels2 = {'0' '-10' '-20' '-30' '-40'};
ff = findall(f,'type','text');
t=strtrim(get(ff,'String'));
for r=1:length(rho_labels)
set(ff(strcmp(t,rho_labels{r})),'String',rho_labels2{r})
end
To modify the number of rings, I think think of anything better than modifying polar.m and creating your own custom polarMOD.m function. If you do this you can also do the radial labels as well. In fact, there might be such a function on the MathWorks File Exchange.

How to choose correct value range of green colour from histogram charts

I tried to segment just leaves from plant image. I spend 2 weeks googling about how to extract correct range value for green colour from the histogram of the hue, saturation and value colour space. I used the following codes:
hsv=rgb2hsv(INSUM);
H1=hsv(:,:,1);
figure, imshow(H1);
H2=hsv(:,:,2);
figure, imshow(H2);
H3=hsv(:,:,3);
figure, imshow(H3)
H1(H1(:)==0)=[];
H2(H2(:)==0)=[];
H3(H3(:)==0)=[];
figure,imhist(H1);
figure,imhist(H2);
figure,imhist(H3);
limitUpperH = 0.3; limitLowerH = 0.19;
limitUpperS = 1; limitLowerS = 0.95;
limitUpperV = 0.6; limitLowerV = 0.02;
LOGICH = (hsv(:,:,1)<limitUpperH) & (hsv(:,:,1)>limitLowerH);
LOGICS = (hsv(:,:,2)<limitUpperS) & (hsv(:,:,2)>limitLowerS);
LOGICV = (hsv(:,:,3)<limitUpperV) & (hsv(:,:,3)>limitLowerV);
LOGIC = LOGICH & LOGICS & LOGICV;
figure() imshow(LOGIC);
I choose the limitUpper and limitLower for the H, S and V works for just one image by trial and error and they give good result for just one image, but when I apply these values on other images it doesn’t work.
I read many things and saw many examples about the HSV and image histogram but none say how to extract green range of h, s and v from histogram chart. Can any one help me?

python matplotlib styles: How to make custom colored markers + lines using matlab style format string

I am using MATLAB-style formating to change the style of plot lines, like
mystyle = '-r.'
ax1.plot(x1, y1, mystyle)
ax2.plot(x2, y2, mystyle)
...
axN.plot(xN, yN, mystyle)
which draws red line + red dot markers. But how do I specify grey color for lines + markers using MATLAB string?
If it is not feasible, what mystyle should look like so that I can control style of all plots sharing it?
Simply define all properties separately. As grey doesn't exist as predefined color, you need to use an normalized RGB-vector, like [0.2 0.2 0.2]
x = 1:42;
y = sin(x);
plot(x, y, 'color',[0.2 0.2 0.2],'LineStyle','-','Marker','.')
if you want multiple plots with the same style you can save it an cell array and access it with an comma-separated-list:
grey = [0.2 0.2 0.2];
myStyle = {'color',grey,'LineStyle','-','Marker','.','MarkerEdgeColor',grey,'MarkerFaceColor',grey}
plot(x, y, myStyle{:})
Try:
plot(xdata,ydata,'.-','Color',[.5 .5 .5])