Put label in colorbar
3.019 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Ting-Yu Chueh
il 18 Set 2019
Commentato: Clara Casals Baixas
il 5 Mar 2024
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 Commento
Risposta accettata
Adam
il 18 Set 2019
Modificato: Adam Danz
il 2 Ott 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 Commenti
Adam
il 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
il 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);
Più risposte (1)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!