Display plot/image on fraction of figure area

조회 수: 8 (최근 30일)
Will
Will 2011년 8월 11일
Is there a way to plot to only a portion of a figure window? I want to create a plot in a function and add a fairly large annotation in a text box. However, I'd prefer it if the plot only took up around 80% of the plot and the text box took up 20% (in terms of width). Right now, the text box covers up part of the plot. I can think of some clunky work arounds, like padding my plot so that the text box covers up filler data, but I'd prefer a more straightforward solution if there is one.

채택된 답변

Walter Roberson
Walter Roberson 2011년 8월 11일
Work out the width of the text box, including the width required for the scroll bar; work out the total width available; subtract the two and subtract a margin; set the axes position to be only as wide as is available.
You can find out how wide a text box "would like to be" by setting up a dummy text box, and using textwrap() on the uicontrol together with the proposed string. The resulting control will have an Extent field that will reflect how big MATLAB would "like" the box to be. If you do not plan to make the box as tall as the recommendation, then add a margin of roughly one character-with (19-ish pixels) to take in to account the scroll bar. That information in hand, delete the dummy text box and set() the real box to the dimensions you just calculated.
  댓글 수: 2
Will
Will 2011년 8월 11일
Thanks for the two quick answers. I hadn't realized that the size of the plot was set in the axes properties.
Is there any advantage to using uicontrol() over annotation() to make a text box?
Walter Roberson
Walter Roberson 2011년 8월 11일
uicontrol() makes it easier to change the text.
uicontrol has the possibility of scrolling the text. Though to do that, you should use an edit box instead of a text box, and set the 'enable' property to 'disable' (*not* 'off')
uicontrol makes it easier to calculate the width of the widest line; annotation is more difficult, especially if you change fonts or character set.

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

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2011년 8월 11일
fig=figure;
h_axes=axes('parent',fig,'position',[.1 .1 .8 .7]);
plot(h_axes,1:10);
h_text=uicontrol('style','text','parent',fig,'position',[147 362 227 33]);
set(h_text,'string','This is the text');

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by