Put label in colorbar

3,404 ビュー (過去 30 日間)
Ting-Yu Chueh
Ting-Yu Chueh 2019 年 9 月 18 日
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 件のコメント
Clara Casals Baixas
Clara Casals Baixas 2024 年 3 月 5 日
Try this:
a=colorbar;
a.Label.String = 'Power (db)';

サインインしてコメントする。

採用された回答

Adam
Adam 2019 年 9 月 18 日
編集済み: Adam Danz 2023 年 10 月 2 日
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 件のコメント
Adam
Adam 2023 年 11 月 20 日
@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 2023 年 11 月 20 日
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);

サインインしてコメントする。

その他の回答 (1 件)

Ruger28
Ruger28 2019 年 9 月 18 日
From
doc colorbar
try
a = colorbar;
a.Label.String = 'Power (dB)';
  1 件のコメント
Ting-Yu Chueh
Ting-Yu Chueh 2019 年 9 月 18 日
JR, thanks.
But, I prefer to revrese the orientation of label, which rotate 270 degree, and with more space between unit and lable.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeColormaps についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by