Put label in colorbar

3.295 views (last 30 days)
Ting-Yu Chueh
Ting-Yu Chueh on 18 Sep 2019
I put the label ('Power (dB') in my color bar, and the code is below:
a=colorbar;
ylabel(a,'Power (db)','FontSize',16,'Rotation',270);
However, the label is too close the colorbar (see the figure).
Can anyone help me? Thanks!
Also, how can I to put the different title for each subplot.
  1 Comment
Clara Casals Baixas
Clara Casals Baixas on 5 Mar 2024
Try this:
a=colorbar;
a.Label.String = 'Power (db)';

Sign in to comment.

Accepted Answer

Adam
Adam on 18 Sep 2019
Edited: Adam Danz on 2 Oct 2023
The label object should have a position that you can edit. The rotation of 270 rather than 90 moves it inside the tick labels for some reason, but you can edit e.g.
hColourbar.Label.Position(1) = 3;
to change the x position of the label.
R2023a or later
An update in R2023a now automatically adjust horizontal and vertical alignment when axis labels are rotated to cardinal directions. Here's a demo
cb = colorbar();
ylabel(cb,'Power (db)','FontSize',16,'Rotation',270)
  5 Comments
Adam
Adam on 20 Nov 2023
@Adam Danz may be able to give a more definitive answer, better than me on that one. Recreating the example above and playing around with properties (something I often do, on command line when answer a question here, even when I start with no idea of the answer), which I always recommend anyone to try for understanding what is possible, I don't see a way to do this.
Confusingly it is the 'VerticalAlignment' property of the label that moves it left-right rather than the 'HorizontalAlignment' (because the text is on its side, but still odd!), but none of the options available for this move it to the other side of the colourbar, they just shift it slightly on the right side.
So you probably would have to use the 'Position' to manually to it, and would likely also need to change the 'Position' of the colourbar itself, as well as the label, which is messy.
Adam Danz
Adam Danz on 20 Nov 2023
I'm afraid I don't have a geat general solution for this but I can offer two workarounds.
Work around 1: Set colorbar and ylabel positions
One approach is to set the position of the ylabel and then, as @Adam explained, update the colorbar position to avoid overlap. I'll use yyaxis for this demo because it has right-size tight labels that might overlap with the colorbar label in this context. Notice I set the axes' PositionConstraint property to innerposition. This is to eliminate the need to update the axes' position after it would automatically expand following a change to the color position.
ax = gca();
yyaxis right
cb = colorbar();
yl = ylabel(cb,'Power (db)','FontSize',16);
yl.Position(1) = min(xlim(cb));
yl.VerticalAlignment = 'bottom';
ax.PositionConstraint = 'innerposition';
cb.Position(1) = cb.Position(1) + 0.05;
Work around 2: Use tiledlayout and set ylabel position
Another approach is to put the colorbar in its own tile in a tiledlayout arrangment. This demo creates a 1x6 layout of tiles. The axes consumes the first 5 and the colorbar is placed in the 6th.
figure
tcl = tiledlayout(1,6);
nexttile([1,tcl.GridSize(2)-1])
yyaxis right
cb = colorbar();
yl = ylabel(cb,'Power (db)','FontSize',16);
yl.Position(1) = min(xlim(cb));
yl.VerticalAlignment = 'bottom';
cb.Layout.TileSpan = [1,1];
cb.Layout.Tile = tcl.GridSize(2);

Sign in to comment.

More Answers (1)

Ruger28
Ruger28 on 18 Sep 2019
From
doc colorbar
try
a = colorbar;
a.Label.String = 'Power (dB)';
  1 Comment
Ting-Yu Chueh
Ting-Yu Chueh on 18 Sep 2019
JR, thanks.
But, I prefer to revrese the orientation of label, which rotate 270 degree, and with more space between unit and lable.

Sign in to comment.

Categories

Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!