colorbar tick distance to axes and label position

9 次查看(过去 30 天)
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 个评论
Sijie Huang
Sijie Huang 2023-12-22
@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 2023-12-22
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.

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2023-12-22
  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 个评论
Sijie Huang
Sijie Huang 2023-12-22
编辑:Sijie Huang 2023-12-22
  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 2023-12-22
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).

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by