Matlab - Add a specific tick on a colorbar - matlab

I'm representing a surface using "surf" function, with a colorbar. I would like to keep the default ticks of the colorbar, but add a custom tick on this colorbar, at a specific value (that I could make red to distinguish it from other ticks for example). Any idea on how to add a custom tick like that with keeping existing ticks on the colorbar ?
Thanks

As Luis mentioned in the comments, you can add an additional tick mark like so
h = colorbar;
newTick = 0.75;
h.Ticks = sort([h.Ticks newTick]);
If you want to add a line to the bar, the easiest thing (I think) is to use an annotation which is positioned relative to the figure (the same as the colorbar), so we can overlay it
pos = h.Position;
r = (newTick - min(h.Ticks))/(max(h.Ticks)-min(h.Ticks));
annotation( 'line', pos(1)+[0, pos(3)], [1, 1]*(pos(2)+pos(4)*r), ...
'color', [1,0,0], 'linewidth', 2 );
I'm setting the x position of the annotation to match the left and right sides of the colorbar, and the y position to match the bottom plus the relative % of the height according to the tick value.
Result:
Similarly, you could use annotatation exclusively to just get a red label, it's a little more convoluted to get everything lined up correctly, you have to make sure the text box is wide enough to be on a single line and vertically aligned to the middle to get the position right:
h = colorbar;
newTick = 0.75;
pos = h.Position;
r = (newTick - min(h.Ticks))/(max(h.Ticks)-min(h.Ticks));
h = 0.2;
annotation( 'textbox', [pos(1)+pos(3)/2, (pos(2)+pos(4)*r)-(h/2), pos(3)*2, h], ...
'color', [1,0,0], 'string', ['- ' num2str(newTick)], 'linestyle', 'none', ...
'VerticalAlignment', 'middle' );

Related

Making a colorbar's frame white while keeping the labels black

How can I make the rectangular line around the box of the colorbar white, but keep the tick labels black?
Reproducible code below:
colormap(flipud(autumn(6))); % Create Colormap
cb = colorbar; % Create Colorbar
cb.Ticks = [.1667 .3334 .5001 .6668 .8335]; % Create ticks
cb.TickLabels = ({'0%','5%','10%','15%','20%'});
cb.TickLength = 0;
cb.Color = 'w'; % Everything becomes white, I only want the rectangular color of the box to be white
You could get the wanted visual effect by overlaying a white annotation box on the colorbar, e.g.
colormap(flipud(autumn(6))); % Create Colormap
cb = colorbar; % Create Colorbar
cb.Ticks = [.1667 .3334 .5001 .6668 .8335]; % Create ticks
cb.TickLabels = ({'0%','5%','10%','15%','20%'});
cb.TickLength = 0;
% cb.Color = 'w'; % Everything becomes white, I only want the rectangular color of the box to be white
annotation('rectangle',cb.Position,'Color','w','FaceColor','none','LineWidth',2)
From the ColorBar properties documentation page we learn that the Color property changes too many things:
Color — Color of tick marks, text, and box outline
So we have to try something else. Fortunately, if you are comfortable with using an undocumented feature, the desired effect can be achieved by modifying the colorbar's Ruler property:
function [] = q66408322()
hF = figure();
hAx = axes(hF);
colormap(hAx, flipud(autumn(6)));
hCb = colorbar(hAx,...
'Ticks', [.1667 .3334 .5001 .6668 .8335],...
'TickLabels', {'0%','5%','10%','15%','20%'}, ...
'TickLength', 0, ...
'Color', 'w');
hCb.Ruler.Color = 'k'; % <<<<<< Solution
Resulting in:

Matlab: Replicate legend location's 'outside' scaling behavior

The preceding figure was produced by the following code:
hold on;
plot([1,2,3,4],[1,2,3,4]);
plot([1,2,3,4],[4,3,2,1]);
legend('foo', 'bar', 'location', 'eastoutside');
Re-scaling the width of the figure window causes the legend to maintain it's dimensions, while automatically scaling the plot's width to take up the extent of the remaining space:
When editing the position properties of the legend, the location property is changed to 'none', losing its unique scaling behavior.
Is there any way to reproduce the scaling behavior in such a way that I could resize/re-position the legend and/or use it for a non axis-legend relationship?
You can get the position of the axes and set the position of the legend relative to them. Here is an example:
x = -10:10;
fig = figure(1);
plot(x,x.^2,x,x.^3);
hL = legend('foo','bar');
% setting the position to the bottom right corner of the axes:
ax = gca;
hL.Position(1:2) = [sum(ax.Position([1 3]))-hL.Position(3) ax.Position(2)];
To keep the position updated upon resizing of the figure you can assign the position set to the SizeChangedFcn property of the figure:
fig.SizeChangedFcn = ...
'hL.Position(1:2) = [sum(ax.Position([1 3]))-hL.Position(3) ax.Position(2)];';
any resize of the figure will update the legend position.

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 shift position of colorbar in 3d plot

I'm displaying a 3D scatterplot, but label on the z-axis is getting overlapped by the color bar. How can I shift the color bar a desired number of pixels to the right or left?
You can use findobjto get an handle to the colorbar, query its current position via get and then modify according to your needs and change it using set:
h = findobj('tag', 'Colorbar');
pos = get(h, 'position')
% modify pos according to your needs
set(h, 'position', pos)

Make background of annotation box in image semi-transparent

I am currently working on adding annotations to satellite images in MATLAB. Since the color underneath each text field can vary quite a lot, I want to use a background color under the text to make it easier to see and read.
However, when I do this, a lot of the terrain gets obscured. I though of trying to make the background color for each text box semi transparent, but hit a dead end trying to come up with a solution.
Any ideas? I was hoping for some UI-element where I could just set the 'facealpha' to maybe 0.5. I also need the text to support being rotated (as can be seen in the examples below).
Below is some example code and the resulting image. The workspace with the satellite data can also be found in the link:
Example workspace
figure(1);clf
imagesc(xx,yy,Map);
hold on
plot(xInspection,yInspection,'g.-')
% # Two ways of making a rotated text annotation.
% # Cant make background semi-transparent
testAnno= annotation('textarrow',[0.5 0.5],[0.5 0.5], ...
'string','textarrow annotation', ...
'HeadStyle','none','LineStyle', 'none',...
'TextRotation',asin(directionVec(1))*180/pi,...
'TextBackgroundColor',[0.7 0.7 0.7]);
testText = text(mean(xInspection),mean(yInspection),'text annotation', ...
'rotation',asin(directionVec(1))*180/pi, ...
'HorizontalAlignment','right', ...
'color',[0 0 0], ...
'backgroundcolor',[0.7 0.7 0.7], ...
'fontsize',8);
It doesn't look like either annotation or text return HgObjects that have BackgroundAlpha properties (they might exist but I wasn't able to find them using getundoc or by trying various different hacks).
I was able to get something working by drawing the background myself. Here is a simple proof of concept:
f = figure;
tObj = text(.5, .5, 'text object', 'FontSize', 20);
set(gca,'XLimMode', 'manual', 'YLimMode', 'manual'); % prevent the axes from resizing automatically
p = get(tObj, 'Extent'); %Get the outer position of the text
% now create a patch around the text object
pObj = patch([p(1) p(1) p(1)+p(3) p(1)+p(3)], [p(2) p(2)+p(4) p(2)+p(4) p(2)], 'r');
uistack(tObj, 'top'); % put the text object on top of the patch object
set(pObj , 'FaceAlpha', .2); % set the alpha of the patch face to .2
%Rotate the objects
set(tObj, 'Rotation', 20);
rotate(pObj, [0 0 1], 20);
I am afraid the only way you can do this is by not setting any color to your annotations, and then placing a patch in the background of each annotation. So something like this:
% Use completely transparent annotations
hA = annotation('textarrow', ..., 'TextBackgroundColor', 'none')
% Place a transparent patch exactly in the background of your annotation
hP = patch(X, Y, 'white', 'EdgeColor', 'none', 'FaceColor', 'white', ...
'alpha', 0.3)
% Ensure that your annotation is on top
uistack(hA, 'top')
But of course the big problem is to determine the correct coordinates of the patch (X and Y). Rotating is easy by simply multiplying your coordinates by a rotation matrix. However, finding the length and height of the patch and its central location is not that easy. You might be able to find some useful functions for this at Matlab central...