How To Label Colormaps in MATLAB? - matlab

I have the following image derived from imagesc(some matrix whose entries correspond to those colors). The Cyan and the Yellow both mean different things. I would like to either:
Add a legend where I can fill in what each color means
Segregate parts of the X-axis to where I can type "cyan" on the x region below the cyan part, and "yellow" on the x region below the yellow part.
Either or would be fine, and which ever one is easier would be appropriate for me.
CYAN YELLOW

Do you want something like this? It's very basic haha.
clc
clear
close all
%// Dummy array
A = repmat([0 0 0 1 1 1],6,1);
imagesc(A)
hold on
%// Dummy data to add legend
scatter(0,0,1,'b','filled')
scatter(0,0,1,'r','filled')
axis off
colorbar
%// Get axis coordinates (x first and then y)
ax = axis;
%// Add text. You can easily adjust the x-offset depending on how many colors you have.
text(ax(2)/4+ax(1),ax(4)+.2,'Blue','Color','b','FontSize',20,'HorizontalAlignment','Center')
text(3*ax(2)/4+.2,ax(4)+.2,'Red','Color','r','FontSize',20,'HorizontalAlignment','Center')
%// Add legend
legend({'Blue';'Red'})
Output:

Here's another option, which happens to be matlab-hg2 friendly:
%% // Initialization
clear variables; close all force; clc;
%% // Generate some data
fakeData = magic(3)-0.5;
fakeData_horz = fakeData(:)'; %//'
fakeNames = cellstr(strcat('color',num2str((1:9)'))); %//'
fakeNameMapping = fakeNames(randperm(numel(fakeData)));
%% // Create figure
hFig = figure('Position',[680,488,758,610],'Resize','off');
%% // Top left example
cLims = [0 numel(fakeData)+1];
hSp = subplot(2,2,1);
imagesc(fakeData); axis image; set(hSp,'XTick',[],'YTick',[]);
colorbar; caxis(cLims);
[XX,YY] = meshgrid(1:size(fakeData,1),1:size(fakeData,2));
text(XX(:),YY(:),fakeNameMapping,'HorizontalAlignment','center');
%% // Bottom example
hSp = subplot(2,2,3:4);
cLims = [0 numel(fakeData)+1]; %Not required here since unchanged
imagesc(fakeData_horz); axis image; set(hSp,'XTick',[],'YTick',[]);
colorbar; caxis(cLims);
drawnow; %// This command will allow the annotations to be positioned properly
for ind1=1:numel(fakeData_horz)
newPos = [hSp.Position(1)+hSp.Position(3)/numel(fakeData_horz) * (ind1-1),...
hSp.Position(2)*1.6,... %1.6 is chosen for the demo
hSp.Position(3)/numel(fakeData_horz),...
0.05]; % 0.05 is chosen for the demo; play around with it
h= annotation('textbox',newPos,'String',fakeNameMapping{ind1},...
'LineStyle','none','HorizontalAlignment','center');
end
%% // Top right example
hSp = subplot(2,2,2);
cLims = [0 numel(fakeData)]; %// cLims is a bit different here!
imagesc(fakeData); axis image; set(hSp,'XTick',[],'YTick',[]);
caxis(hSp,cLims); colormap(hSp,parula(numel(fakeData)));
cb = colorbar; %// This time we need a handle to the colorbar
cb.Ticks = (hSp.CLim(1):hSp.CLim(2))+0.5; %// Set the tick positions
cb.TickLabels = fakeNames; %// Set the tick strings
Which results in:
Note: unless using a more intelligent text positioning computation, the figure's size should not be changed after it was plotted (in the 2nd example), because then the text no longer remains where it should be.
Edit: added another option where only the colorbar is labeled.

Related

Change color of specific points in matlab?

I have a scatterplot in Matlab and was wondering if there was any way to the change the color of just one of the points?
you can plot the graph, after that replot the point which you want.
% plot the curve or graph
hold on
plot(x,y,'.r')
Try to hold on and then plot the point (x,y) which you want with the specified color (r) which you are interested in.
If you do not want to overlay a second plot on the first, you can plot each point individually and use handles. In that way, you can later perform arbitrary changes on each individual point.
You can find an example below.
% Generate some numbers
x = randn(10,1);
y = randn(10,1);
% Plot each point individually
figure
hold on
for idx = 1 : numel(x)
hdl(idx) = plot(x(idx),y(idx),'marker','.','color','k')
end
% change color, markerstyle, x-position, etc...
hdl(2).Color = [1 0 0]
hdl(3).Marker = 'o'
hdl(5).XData = 1
x = rand(10,1) ;
y = rand(10,1) ;
scatter(x,y) ;
[x1,y1] = getpts ;
hold on
plot(x1,y1,'Or') ;
click on the point, you want to change color, when prompted.

Specifying different colour schemes for different orientations of Polarhistogram in MATLAB

Question
When using polarhistogram(theta) to plot a dataset containing azimuths from 0-360 degrees. Is it possible to specify colours for given segments?
Example
In the plot bellow for example would it be possible to specify that all bars between 0 and 90 degrees (and thus 180-270 degrees also) are red? whilst the rest remains blue?
Reference material
I think if it exists it will be within here somewhere but I am unable to figure out which part exactly:
https://www.mathworks.com/help/matlab/ref/polaraxes-properties.html
If you use rose, you can extract the edges of the histogram and plot each bar one by one. It's a bit of a hack but it works, looks pretty and does not require Matlab 2016b.
theta = atan2(rand(1e3,1)-0.5,2*(rand(1e3,1)-0.5));
n = 25;
colours = hsv(n);
figure;
rose(theta,n); cla; % Use this to initialise polar axes
[theta,rho] = rose(theta,n); % Get the histogram edges
theta(end+1) = theta(1); % Wrap around for easy interation
rho(end+1) = rho(1);
hold on;
for j = 1:floor(length(theta)/4)
k = #(j) 4*(j-1)+1; % Change of iterator
h = polar(theta(k(j):k(j)+3),rho(k(j):k(j)+3));
set(h,'color',colours(j,:)); % Set the color
[x,y] = pol2cart(theta(k(j):k(j)+3),rho(k(j):k(j)+3));
h = patch(x,y,'');
set(h,'FaceColor',colours(j,:),'FaceAlpha',0.2);
uistack(h,'down');
end
grid on; axis equal;
title('Coloured polar histogram')
Result

How to align colorbar tick labels and lines in Matlab

I need to make a plot with a discrete colorbar in Matlab. I do this in the following way:
data = randi(10, 20);
imagesc(data)
my_colormap = rand(10, 3);
colormap(my_colormap)
cb = colorbar
set(cb,'YTickLabel',{'A';'B';'C';'D';'E';'F';'G';'H';'I';'J';})
Now my problem is that the colorbar tick labels and the small lines in the colorbar don't align nicely. How can I even the colorbar tick labels and the small lines better as illustrated in the following pic:
The TickLabel on the colorbar each correspond to a value (a Tick). To place the TickLabels in the middle, you need to place the tick in the middle. To make this dynamic (so that It does not change when resizing the image) was I bit tricky I recall and I do not really recall. To set the ticks just once is not so hard though,
set(hCbar,'YTicks',RightYTicks);
EDIT:
On request I will post an example. This should give a hint of what to do.
x = 1:10;
y = 1:10;
cmap = jet(10);
[x, y] = meshgrid(x,y); %x and y grid
c = x-0.1; %Set color code to increase to the right
hFig = figure;
scatter(x(:),y(:),10,c(:),'filled'); % Simpler for the example
set(gca(hFig),'CLim',[0,10]);
colormap(cmap);
hCbar = colorbar;
set(hCbar,'YTicks',0.5:9.5);
set(hCbar,'YTickLabels',{'A','B','C','D','E','F','G','H','I','J'});
For newer matlab version, the YTicks may have changed name to Ticks And YTickLabels may be called TickLabels.

Matlab Colorbar: in scientific notation, the multiplier 10^-3 overlaps the first value.How to move it?

I have a Matlab plot, in which the multiplier (in my case 10^-3) overlaps the first value. How can I move it?
Here is a hack which works but could become a bit cumbersome. The trick is to fetch the YTickLabel of the colorbar, remove them from the plot, then plot them again but this time add a custom text that represents the exponent you want to display (here x 10^-3). The plus side is that you have complete control over the placement of this text.
Here is the code:
clear
clc
clear all; close all; clc;
A = rand(100,100)./(1e2);
figure;
imagesc(A);
colormap jet;
hBar= colorbar;
title('Before change','FontSize',18)
%// Get the positions of the axes and colorbar as well as the YTickLabel.
BarPos = get(hBar,'Position');
XL = get(gca,'XLim');
YTL = get(hBar,'YTickLabel');
figure;
imagesc(A);
colormap jet;
hBar= colorbar;
%// Remove current YTickLabel
set(hBar,'YTickLabel','');
%// Text to add. Note the syntax to print a superscript.
NewText = 'x 10 ^{-3}';
%// Restore YTickLabel. This time the x 10^-3 does not appear.
set(hBar,'YTickLabel',YTL);
%// Add the text
text(XL(2)+15,-5,NewText)
title('After change','FontSize',18)
Output:
Hope that helps!

How to set colorbar labels

I have some points in a 'jet' colormap. The points have a coefficient that can go from 0 to 1, but usually they dont cover all the range, e.g 0.75-0.9.
When I plot those points I colour them so 0.75 is the lesser colour in the colormap and 0.9 is the maximum color in the colormap, so all the colormap is shown. What I want to do is show that in the colorbar also. When I plot the colorbar the labels on it go to 64, but I want them from 0.75 to 0.9. How can I do that?
EDIT
I don't think the code itself helps a lot but here it goes, just in case. In the colors variable I convert the ZNCC to the range of the colormap.
EDIT2
I found the reason why caxis is not working for me. Here is the code:
%this is why it doesnt work
im=imread('someimageyouwanttotest_inRGB.png')
imshow(im)
points=[1, 2;1 , 2 ;0.3,0.7]
ZNCC=points(3,:)
cmap=colormap('jet');
colors=cmap(round( ((1-min(ZNCC))+ZNCC-1).*(size(cmap,1)-1)/max((1-min(ZNCC))+ZNCC-1))+1,: );
hold on
for i=1:length(ZNCC)
plot(points(1,i),points(2,i),'.','Color',colors(i,:));
end
colorbar()
hold off
I think that is your code displays all your colours correctly then rather just set up the colour bar first on no image:
points=[1, 2;1 , 2 ;0.3,0.7]
ZNCC=points(3,:)
cmap=colormap('jet');
caxis([min(ZNCC) max(ZNCC)]);
colorbar();
hold on
%this is why it doesnt work
im=imread('someimageyouwanttotest_inRGB.png')
imshow(im)
colors=cmap(round( ((1-min(ZNCC))+ZNCC-1).*(size(cmap,1)-1)/max((1-min(ZNCC))+ZNCC-1))+1,: );
for i=1:length(ZNCC)
plot(points(1,i),points(2,i),'.','Color',colors(i,:));
end
hold off
I can't test it as I don't have imshow :/
If caxis is not working for you, you could store the return from colorbar - it is a handle to the colorbar object. Then you can set its properties, like 'YTick' and 'YLim'. The full list of properties you can set is the same as the Axes Properties (because the colorbar is just an axes object, after all).
Here is an example:
% Generate some random data
z = rand(10);
[x, y] = meshgrid(1:size(z, 1));
% Plot colour map
pcolor(x, y, z);
shading interp; % Comment out to disable colour interpolation
colormap jet;
% Setup colorbar
c = colorbar();
set(c, 'YTick', [0.75 0.875 1]); % In this example, just use three ticks for illustation
ylim(c, [0.75 1]);
It is only necessary to do this once, after you've finished plotting.
Edit: If you need the limits and ticks automatically from the data, then you can do something like
% Find the limits
lims = [min(z(:)) max(z(:))];
% Function for rounding to specified decimal places
dprnd = #(x, dps)round(x*(10.^dps))./(10.^dps);
% Generate ticks
nTicks = 5;
nDps = 2;
ticks = dprnd(linspace(lims(1), lims(2), nTicks), nDps);
set(c, 'YTick', ticks);