MATLAB Answers


Ensure x label never cut off

Edward Umpfenbach さんによって質問されました 2012 年 5 月 4 日
最新アクティビティ Opt User
さんによって コメントされました 2018 年 2 月 12 日
I have a bar plot with some x labels that can be fairly lengthy (rotated vertically with rotateticklabel). If left on its own, Matlab cuts off the x labels.
How can I set the figure to "squeeze" the bars so that all of the labels are visible?

  0 件のコメント

サインイン to comment.

2 件の回答

Daniel Shub
回答者: Daniel Shub
2012 年 5 月 4 日

A "manual" method of fixing cutoff labels by making the axis smaller is relatively easy to implement. You pick a scale value that makes the labels not be cutoff.
scale = 0.9;
pos = get(gca, 'Position');
pos(2) = pos(2)+scale*pos(4);
pos(4) = (1-scale)*pos(4);
set(gca, 'Position', pos)
A more automated solution where you programmatically find the lower edge of the tick labels is more involved. Once you have the lower edge you need to decide if you want to shrink the axis or enlarge the figure. There of course exists the possibility that the tick labels are so long that you cannot shrink the axis enough. A less likely problem is that your tick labels are larger than the screen. This requires another work around ( ).
A fully automated solution is difficult. The easiest solution would be to take the programmatic solution and include it in a modified rotateticklabel.

  1 件のコメント

Edward Umpfenbach 2012 年 5 月 4 日
Thanks. Should be sufficient for my needs.

サインイン to comment.

回答者: Tilen Thaler 2013 年 2 月 20 日

Dear Mr. Umpfenbach,
There is another method which mingles with Position and OuterPosition in the active axes.
For more details on the Position and OuterPosition axes properties please check Matlab help. But to summarize, here is a sample code:
% get current (active) axes property
If your label is cut off one of the elements of the four-element vector OuterPosition = [left bottom width height] should be modified. If your x label is cut off, then perhaps element bottom is too low. Increase it:
set(gca,'OuterPosition',[left bottom + 0.1 width height])
Note that the vector is in normalized units but you may change that to suit your needs.
Perhaps it might happen that title gets cut off in the process. In this case increase height as well.

  1 件のコメント

Opt User
2018 年 2 月 12 日
This does not work for me, since it crops my figure on top.

サインイン to comment.

Translated by