Figure Axis formatting; how to move label from axis

조회 수: 443 (최근 30일)
jfrazie9
jfrazie9 2019년 3월 13일
댓글: Adam Danz 2021년 5월 12일
Hello, I am trying to create my own x axis value labels on a logarithmic scale. To do this I am trying to move the label down, thus creating space between the edge of the plot and the x axis title. I then would like am making annotation boxes with the values I want as my labels. I can create the annotation boxes and move them but am unable to create the space between the axis label and x axis title. Any help is greatly appreciated.

채택된 답변

Adam Danz
Adam Danz 2019년 3월 13일
편집: Adam Danz 2019년 4월 3일
To change the position of the x label, store the handle to the xlabel, then change its position property. By default the axis labels are in 'data' units. If you want to move the x label down, you'll want to subtract from the y-coordinate of the x label position.
xlh = xlabel('x label');
xlh.Position(2) = xlh.Position(2) - 0.1; % move the label 0.1 data-units further down
In this demo below, the x label is moved down by 10% of its distance to the axes. Note that the x label position could have a positive or negative y-value so abs() is needed in order to always offet toward -inf.
figure
axh = axes;
xlh = xlabel('x label');
xlh.Position(2) = xlh.Position(2) - abs(xlh.Position(2) * 0.1);
Caution: if you move the x label too far, it may no longer be visible.
  댓글 수: 2
giannit
giannit 2021년 5월 12일
편집: giannit 2021년 5월 12일
Doesn't work with heatmap, is there another way? Thanks
Adam Danz
Adam Danz 2021년 5월 12일
Heatmap axes are a different species and are difficult to customize. But that won't stop us 😎
To access the xlabel handle you have to use an undocumented method that will throw a warning but since we don't like to see warnings, it will be temporarily suppressed.
% h is the heatmap handle obtained by h=heatmap(__)
% Temporarily suppress warning associated with undocumented
% access to axes handle
origState = warning('query', 'MATLAB:structOnObject');
warningCleanup = onCleanup(@()warning(origState));
warning('off','MATLAB:structOnObject')
S = struct(h); % where h is heatmap handle
clear('warningCleanup')
xlh = S.Axes.XLabel; % <--- xlabel handle
Now you can set it's position properties as shown in my answer.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by