Adding a legend when using imagesc, including white for NaN - matlab

I have a 35x43 matrix of data with vales ranging from 1-6 and lots of NaNs.
I want to NaNs to be white, and the numbers to each be a different colour. I need a legend with the 6 different colour and labels on it.
I can achieve most of this with the following code, however the colours in the legend do not match the colours in the figure. See code below
figure(6)
subplot(1,2,1)
imagesc(lut)
title('Highest Weighted Predictor variable for each Pixel')
ylabel('Longitude')
xlabel('Latitude')
caxis([0, 7])
myColorMap = jet(7);
myColorMap(1,:) = 1;
colormap(myColorMap);
M = jet(7); % Notice the 3, here and below
hold on
L = line(ones(7),ones(7));
set(L,{'color'},mat2cell(M,ones(1,7),3))
[legh,objh,outh,outm] = legend('First','Second','Location','Southeast');
set(objh,'linewidth',200);
legend('Forest','Shrubland','Savanna','Grassland','Agricultural','Barron');
grid on
ax = gca
ax.GridAlpha = .2
ax.XTick = [5 10 15 20 25 30 35 40];
ax.YTick = [5 10 15 20 25 30];
ax.XTickLabel = {'118^{o}E','123^{o}E','128^{o}E', '133^{o}E', '138^{o}E', '143^{o}E','148^{o}E', '153^{o}E'};
ax.YTickLabel = {'13^{o}S','18^{o}S','23^{o}S','28^{o}S','33^{o}S','38^{o}S'};
ax.TickLength =[0.0 0.0]

To display the NaN values as white I would use something like this. Then for your colormap, just use jet(6). Then the colors will match up just fine.
lut = [1:6 NaN];
myColorMap = jet(6);
imagesc(lut, 'AlphaData', ~isnan(lut))
colormap(myColorMap);
L = line(ones(6), ones(6));
set(L, {'Color'}, num2cell(myColorMap, 2))
legend(L, {'Forest','Shrubland','Savanna','Grassland','Agricultural','Barron'})

I would suggest an alternative solution using a colorbar with individual ticks:
%// example data
lut = randi(6,35,43);
lut(1:23:end) = NaN;
%// parts of your your code
figure(6)
% subplot(1,2,1)
imagesc(lut)
title('Highest Weighted Predictor variable for each Pixel')
ylabel('Longitude')
xlabel('Latitude')
caxis([0, 7])
myColorMap = jet(7);
myColorMap(1,:) = 1;
colormap(myColorMap);
M = jet(7); % Notice the 3, here and below
hold on
%// colorbar
c = colorbar
c.Ticks = (1:6)+0.5
c.TickLabels = {'Forest','Shrubland','Savanna','Grassland','Agricultural','Barron'}

Related

YTickLabel will be shown with some specified value (not show all) in Matlab

I have code and the result as below:
%% How to plot each matrix in a cell in 3d plot(1 matrix with 1 color) ?
% Generate Sample data cell A(1x10 cell array)
clear; clc;
A = cell(1,10); % cell A(1x10 cell array)
for kk = 1:numel(A)
z = 10*rand()+(0:pi/50:10*rand()*pi)';
x = 10*rand()*sin(z);
y = 10*rand()*cos(z);
A{kk} = [x,y,z];
end
A_6 = A(1:6); % generate a cell with the first 6 matrices in "A" cell array
% The numer "6" can be changed to be any number which you want to plot them by colormap
newA = vertcat(A_6{:}); %Concatenating all matrices inside A vertically
numcolors = numel(A_6); %Number of matrices equals number of colors
colourRGB = hsv(numcolors); %Generating colours to be used using hsv colormap
colourtimes = cellfun(#(x) size(x,1),A_6);%Determining num of times each colour will be used
colourind = zeros(size(newA,1),1); %Zero matrix with length equals num of points
colourind([1 cumsum(colourtimes(1:end-1))+1]) = 1;
colourind = cumsum(colourind); %Linear indices of colours for newA
scatter3(newA(:,1), newA(:,2), newA(:,3), [], colourRGB(colourind,:),'filled');
%if you want to specify the size of the circles, use the following line instead:
% scatter3(newA(:,1), newA(:,2), newA(:,3), colourind , colourRGB(colourind,:),'filled');
grid on;
view(3); %view in 3d plane
colormap(colourRGB); %using the custom colormap of the colors we used
%Adjusting the position of the colorbar ticks
caxis([1 numcolors]);
colorbar('YTick',[1+0.5*(numcolors-1)/numcolors:(numcolors-1)/numcolors:numcolors],'YTickLabel', num2str([1:numcolors]'), 'YLim', [1 numcolors]);
I have the image like this:
How can I shown "YTickLabel" with some specified value (not show all) as below figure?
Taking the code from your comment to the previous answer:
h = colorbar('YTick',[1:numcolors],'YTickLabel', num2str([1:numcolors]'), 'YLim', [1 numcolors]); set(h, 'Ticklabels', {1 [] 3 [] 5 6}); set(h, 'Ticks', {1 3 5 6});
You are now setting 6 tick labels for only 4 ticks. If set the ticks to the correct values, then the auto-generated tick labels will be what you want.
Try the following:
h = colorbar('YTick',[1:numcolors],'YTickLabel', num2str([1:numcolors]'), 'YLim', [1 numcolors]);
set(h, 'Ticks', {1 3 5 6});
To remove some ticks with their labels, just set the 'YTick' property of the colorbar accordingly. That is, replace your last line by something like:
colorbar('YTick', [1 3 5 6], 'YLim', [1 numcolors])

Visualize a 3D image in Matlab

Think of a 3 dimensional image V stored, in matlab, as an array for red, green, and blue. For pixel (i,j,k) we would have
V(i,j,k,1)=0.1 (red)
V(i,j,k,2)=0.9 (green)
V(i,j,k,3)=0.2 (blue)
I would then like to visualize this 3D image as a moving 2D slice. I thought the "slice" command could do the trick but it is designed to visualize 3D functions only, not 3D images. Do you have any suggestion?
I am adding a script that could help but does not achieve what is requested:
clear all; clc; close all;
N=35;
w=zeros(N,N,N);
x=linspace(-5,5,N); y=linspace(-5,5,N); z=linspace(-5,5,N);
for i=1:N
for j=1:N
for k=1:N
w(i,j,k)=cos(k)/(1+i+j)^2;
end
end
end
for k = 0:.1:10
hsp = surf(linspace(-5,5,Nx),linspace(-5,5,Ny),zeros(Nz));
rotate(hsp,[1,0,0],18*k,[0 0 0]) %rotate a slice where the image is shown
xd = hsp.XData;
yd = hsp.YData;
zd = hsp.ZData;
delete(hsp)
slice(x,y,z,w,xd,yd,zd)
shading flat
hold off
view(-25,20)
axis([-5 5 -5 5 -5 5]);
drawnow
end
The above w array should now be a 3D image instead.
It sounds like you want the functionality of slice but you want it to operate on an [N x M x P x 3] array instead of an [N x M x P] array.
One way to do this is by setting up an interpolator for each channel:
red = griddedInterpolant({x,y,z},V(:,:,:,1));
green = griddedInterpolant({x,y,z},V(:,:,:,2));
blue = griddedInterpolant({x,y,z},V(:,:,:,3));
and then replacing
slice(x,y,z,w,xd,yd,zd)
with
cdata = cat(3, red(xd,yd,zd), green(xd,yd,zd), blue(xd,yd,zd));
surface(xd,yd,zd,cdata);
I tried this on your example and found that the colors in w were all very close to black. So I loaded a different image to work on:
% An orange-and-black skull for Halloween
load mri
V = permute(double(D), [1 2 4 3]);
V = V / max(V(:));
V = bsxfun(#times, V, reshape([1 .3 0], [1 1 1 3]));
% Define grid coordinates and interpolator
[Nx, Ny, Nz, nCh] = size(V);
x = linspace(-5,5,Nx);
y = linspace(-5,5,Ny);
z = linspace(-5,5,Nz);
red = griddedInterpolant({x,y,z},V(:,:,:,1));
green = griddedInterpolant({x,y,z},V(:,:,:,2));
blue = griddedInterpolant({x,y,z},V(:,:,:,3));
for k = 0:.1:10
hsp = surf(linspace(-5,5,Nx),linspace(-5,5,Ny),zeros(Ny,Nx));
rotate(hsp,[1,0,0],18*k,[0 0 0])
xd = hsp.XData;
yd = hsp.YData;
zd = hsp.ZData;
delete(hsp)
cdata = cat(3, red(xd,yd,zd), green(xd,yd,zd), blue(xd,yd,zd));
surface(xd,yd,zd,cdata)
shading flat
hold off
view(-25,20)
axis([-5 5 -5 5 -5 5]);
drawnow
end

Make squared in legend instead of lines Matlab

I have the following code, which plots a 'map' using imagesc, and provides a legend, see output attached.
I am trying to replace the lines in the legend with solid squares. My attamps to far leave the lines and ad hollow squares (including a random square in the top left corner of the figure)
figure(6)
imagesc(lut)
title('Ditribution of Land use Types')
ylabel('Longitude')
xlabel('Latitude')
caxis([0, 7])
myColorMap = jet(6);
imagesc(lut, 'AlphaData', ~isnan(lut))
colormap(myColorMap);
L = line(ones(6), ones(6));
set(L, {'Color'}, num2cell(myColorMap, 2))
legend(L, {'Forest','Shrubland','Savanna','Grassland','Agricultural','Barron'})
set(L(:),'Marker','s')
grid on
ax = gca
ax.GridAlpha = .2
ax.XTick = [5 10 15 20 25 30 35 40];
ax.YTick = [5 10 15 20 25 30];
ax.XTickLabel = {'118^{o}E','123^{o}E','128^{o}E', '133^{o}E', '138^{o}E', '143^{o}E','148^{o}E', '153^{o}E'};
ax.YTickLabel = {'13^{o}S','18^{o}S','23^{o}S','28^{o}S','33^{o}S','38^{o}S'};
ax.TickLength =[0.0 0.0]
Use nan to create invisible data (thanks #matlatbgui), and set L with all needed properties for no line and filled square markers:
% some arbitrary data:
N = 30;
lut = diag(1:N)*ones(N)+(diag(1:N)*ones(N)).';
% coloring settings:
caxis([0, 7])
myColorMap = jet(6);
% plotting:
imagesc(lut, 'AlphaData', ~isnan(lut))
colormap(myColorMap);
% Setting the legend:
L = line(nan(6), nan(6),'LineStyle','none'); % 'nan' creates 'invisible' data
set(L, {'MarkerEdgeColor'}, num2cell(myColorMap, 2),...
{'MarkerFaceColor'},num2cell(myColorMap, 2),... % setting the markers to filled squares
'Marker','s');
legend(L, {'Forest','Shrubland','Savanna','Grassland','Agricultural','Barron'})
and you don't need your line:
set(L(:),'Marker','s')
The square on the upper-left corner is obviously due to set(L(:),'Marker','s') which draws a square at the start and end points of the lines, at [1, 1]. Instead of changing the 'Marker', if you increase the 'LineWidth', you get much better results with:
L = line(ones(6), ones(6));
legend(L, {'Forest','Shrubland','Savanna','Grassland','Agricultural','Barron'})
set(L(:), 'LineWidth', 10)
With this output:
So if you are not restricted to make squares, I believe wide rectangles are better flags for color.

MATLAB Multiple(parallel) box plots in single figure

I'm using the boxplot function in MATLAB. I need to plot boxplots for 6 different datasets for 6 'XTicks' i.e each tick in the x axis should contain 6 corresponding boxes, whiskers, median lines and set of outliers within it's domain. I tried manipulating the 'XTick' property by setting offsets for each variable, but it doesn't apply for boxplot() as it would for a normal plot(). I'm also not able to add legends.
A 3 variable equivalent of my problem would like the following:
Edit:
The following is the code snippet that needs to be modified
TreadmillData = randi([20,200],69,6);
Speeds = {'1.5mph' '2.5mph' '3.5mph' '4.5mph' '5.5mph' '6.5mph'};
DeviceColors = {'r' 'g' 'c' [0.5 0 0.5] 'b' [1 0.5 0]};
Pedometer1 = TreadmillData(1:7:end,:);
Pedometer2 = TreadmillData(2:7:end,:);
Pedometer3 = TreadmillData(3:7:end,:);
Pedometer4 = TreadmillData(4:7:end,:);
Pedometer5 = TreadmillData(5:7:end,:);
Pedometer6 = TreadmillData(6:7:end,:);
GroupedData = {Pedometer1 Pedometer2 Pedometer3 Pedometer4 Pedometer5 Pedometer6};
legendEntries = {'dev1' 'dev2' 'dev3' 'dev4' 'dev5' 'dev6'};
figure;
Xt = 20:20:120;
Xt_Offset = [-15,-10,-5,5,10,15];
for i=1:6
boxplot(GroupedData{i},'Color',DeviceColors{i});
set(gca,'XTick',Xt+Xt_Offset(i));
if i==3
set(gca,'XTickLabel',Speeds);
end
hold on;
end
xlabel('Speed');ylabel('Step Count'); grid on;
legend(legendEntries);
Any help would be appreciated!
I've made some modifications to your code. I've tested this in R2014b.
TreadmillData = randi([20,200],69,6);
Speeds = {'1.5mph' '2.5mph' '3.5mph' '4.5mph' '5.5mph' '6.5mph'};
DeviceColors = {'r' 'g' 'c' [0.5 0 0.5] 'b' [1 0.5 0]};
Pedometer1 = TreadmillData(1:7:end,:);
Pedometer2 = TreadmillData(2:7:end,:);
Pedometer3 = TreadmillData(3:7:end,:);
Pedometer4 = TreadmillData(4:7:end,:);
Pedometer5 = TreadmillData(5:7:end,:);
Pedometer6 = TreadmillData(6:7:end,:);
GroupedData = {Pedometer1 Pedometer2 Pedometer3 Pedometer4 Pedometer5 Pedometer6};
legendEntries = {'dev1' 'dev2' 'dev3' 'dev4' 'dev5' 'dev6'};
N = numel(GroupedData);
delta = linspace(-.3,.3,N); %// define offsets to distinguish plots
width = .2; %// small width to avoid overlap
cmap = hsv(N); %// colormap
legWidth = 1.8; %// make room for legend
figure;
hold on;
for ii=1:N %// better not to shadow i (imaginary unit)
%if ii~=ceil(N/2)
% labels = repmat({''},1,N); %// empty labels
%else
labels = Speeds; %// center plot: use real labels
%end
boxplot(GroupedData{ii},'Color', DeviceColors{ii}, 'boxstyle','filled', ...
'position',(1:numel(labels))+delta(ii), 'widths',width, 'labels',labels)
%// plot filled boxes with specified positions, widths, labels
plot(NaN,1,'color',DeviceColors{ii}); %// dummy plot for legend
end
xlabel('Speed'); ylabel('Step Count'); grid on;
xlim([1+2*delta(1) numel(labels)+legWidth+2*delta(N)]) %// adjust x limits, with room for legend
legend(legendEntries);
Here is a solution for plotting several boxplot. You have to group all the data in a single matrix, each group being separated by a column of Nan. After that, you can simply plot a single regular boxplot with ad-hoc options such as colors and labels.
The following example uses 2 groups of 3, so 7 columns. The 4 first lines of data:
0.6993 0.0207 -0.7485 NaN 0.5836 -0.1763 -1.8468
-0.0494 -1.5411 0.8022 NaN 2.7124 -0.0636 -2.3639
0.9134 0.7106 -0.1375 NaN -0.2200 -0.2528 -0.8350
-0.5655 1.3820 0.6038 NaN -0.7563 -0.9779 0.3789
And the code:
figure('Color', 'w');
c = colormap(lines(3));
A = randn(60,7); % some data
A(:,4) = NaN; % this is the trick for boxplot
C = [c; ones(1,3); c]; % this is the trick for coloring the boxes
% regular plot
boxplot(A, 'colors', C, 'plotstyle', 'compact', ...
'labels', {'','ASIA','','','','USA',''}); % label only two categories
hold on;
for ii = 1:3
plot(NaN,1,'color', c(ii,:), 'LineWidth', 4);
end
title('BOXPLOT');
ylabel('MPG');
xlabel('ORIGIN');
legend({'SUV', 'SEDAN', 'SPORT'});
set(gca, 'XLim', [0 8], 'YLim', [-5 5]);

How to change bars colour in MATLAB

I am new to programming so I am learning introductory to MATLAB I was wondering how you could change colours of bar in MATLAB.
this is my script. Can someone please help!!
x =[1:8]
for y = [20 30 40 50 60 70 80]
bar(x,y)
if y < 40
col = 'b';
else if y > 40
col= 'g';
end
end
end
i also tried bar(x,y, r) but it doesnt work
Whilst this is overkill for your specific question, in general, to change the colour of bars depending on their height, you can apply a colormap to the bars. This is mainly from the bar documentation.
x = 1:8;
y = 10*x;
h=bar(y); %// create a sample bar graph
For the colormap MAP, you do this:
colormap(MAP)
ch = get(h,'Children');
fvd = get(ch,'Faces');
fvcd = get(ch,'FaceVertexCData');
[zs, izs] = sort(y);
for i = 1:length(x)
row = izs(i);
fvcd(fvd(row,:)) = i;
end
set(ch,'FaceVertexCData',fvcd)
hold off
And, for example, using the builtin colormap hsv, you get
But in this case we want a very specific colormap,
b=40 %// the cut-off for changing the colour
MAP=zeros(length(x),3); %// intialise colormap matrix
MAP(y<b,:)=repmat([0 0 1],sum(y<b),1); %// [0 0 1] is blue, when y<40
MAP(y>=b,:)=repmat([0 1 0],sum(y>=b),1); %// [0 1 0] is green, for y>=40
colormap(MAP)
which gives
To use two different colors depending on y: compute a logical index depending on y values and call bar twice with appropriate arguments:
x = [1:8];
y = [20 30 40 50 60 70 80];
ind = y < 40; %// logical index
bar(x(ind), y(ind), 'facecolor', 'b', 'edgecolor', 'k') %// blue bars, black edge
hold on %// keep plot
bar(x(~ind), y(~ind), 'facecolor', 'g', 'edgecolor', 'k') %// green bars, black edge
set(gca,'xtick',x)
bar can take third argument, which is color value, e.g. bar(x,y, 'r').