colorbar tick distance to axes and label position

4 views (last 30 days)
Hi,
I have the following figure generated from MATLAB2023b
by the following code
fig = figure;
fig.Position = [0 900 600 500];
% code for creating the contour
% ...
% ...
pbaspect([18 8 1]);
clim([-0.5 1]);
cbar = colorbar;
cbar.Location = 'north';
cbar.AxisLocation = 'in';
cbar.Ruler.TickLabelFormat = 25;
cbar.Position = [0.5; 0.91; 0.32; 0.03]; % controls the colorbar position
cbar.Label.Interpreter = 'latex';
cbar.Label.String = '$\langle u\rangle$';
cbar.Label.Rotation = 0;
cbar.Label.Position = [-1 -2]; % controls the label position
cbar.Label.Interpreter = 'latex';
cbar.Label.FontSize = 25;
My question is:
  1. how to obtain the distance/relative position from the colorbar ticks to the axes (i.e., the red arrow and green arrow)? The command cbar.Position can only manipulate the position of the colorbar itself; it doesn't include the size of the tick labels.
  2. what's the meaning of the second number in cbar.Label.Position (i.e., cbar.Label.Position(2)), indicated by the blue arrow? For the same value here (which is -2), the position of the label with respect to the colorbar will change if the fig size is changed as well. So it seems it is somehow dependent on the figure size.
  3 Comments
Sijie Huang
Sijie Huang on 22 Dec 2023
@Star Strider Then is it possible to get the axes positions of the colorbar object? So I can adjust my label position accordingly.
Star Strider
Star Strider on 22 Dec 2023
My impression is that you defined the Position of the colorbar, so it should be set as you defined it. The Label position relates to the colorbar itself, since the Label is simply a text object. All those positions are somehow encoded in the figure properties, however exactly how (and how to get or set them) appears not to be documented.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 22 Dec 2023
  1. You can get the position property of both cbar handle, and the axes.
ax = gca
axesPosition = as.Position
2. The Position property has these 4 meanings: [xLeft, yTop, width, height].
If that doesn't get what you want, look at findobj and start looking at what the "children" of the axes represent.
  2 Comments
Sijie Huang
Sijie Huang on 22 Dec 2023
Edited: Sijie Huang on 22 Dec 2023
  1. the cbar.Position does not provide or include the information about the size/position of the tick labels
  2. the cbar.Label.Position only contains three values, rather than the conventional [xLeft, yTop, width, height]
Image Analyst
Image Analyst on 22 Dec 2023
OK, never mind then. I guess they're using a different definition of Position here than they do in most other places then (e.g. the rectangle function).

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!