Place text on a subplot: Any way to autowrap?

I used the "text" command to place text onto a subplot. I have to insert line breaks manually, which I would like to avoid.
The "annotate" command seemed like a solution (https://www.mathworks.com/matlabcentral/answers/218675-is-it-possible-to-create-textbox-with-fixed-width-on-a-plot). However, the 4 position parameters are relative to the entire figure rather than the pair of axes in a subplot.
Is there something like "annotate", but for a pair of axes?

댓글 수: 5

FM
FM 2022년 7월 13일
편집: FM 2022년 7월 13일
With help from [https://www.mathworks.com/matlabcentral/answers/356708-how-to-position-a-annotation-in-subplot-southeast], I supplied "get(gca,'Position')" as the "dim" name/value parameter:
annotation('textbox',get(gca,'Position'),String="The quick brown fox",FitBoxToText='off')
Unfortunately, the text box's left edge overlaps with the y-axis labels!
A work-around that doesn't involve manual customization of the "Position" based on Figure size and number of subplots would be welcome. Thanks!
P.S. I currently finagle the position by manually resizing the text box, issuing "get(gco,'Position'), then editing the "annotation" command to use this position. Ideally this would not be necessary.
Adam Danz
Adam Danz 2022년 7월 14일
편집: Adam Danz 2022년 7월 14일
Use the "innerposition" instead of "position" and make sure the axis and annotation units are "normalized". If the axis size changes relative to the figure, the annotation textbox size will not change unless you add a listener or a LimitsChangedFcn.
I replaced get(gca,'Position') with get(gca,'InnerPosition'), but the result is the same. Note that both get's return the same 4 numbers.
Regarding the use of normalized units, get(gca,"Units") returns 'normalized'. If I select the text box, get(gco,'Units') returns also returns 'normalized'.
To ensure that I'm not changing figure/axes positions or sizes *after* the annotation is placed, I issue the following before the figure exists, followed by subset(2,2,1):
set(gcf,'Position',[982 124 289 274])
It looks like the cause of the problem is still a mystery.
I found that the text box does resize if I resize the figure. As the figure width decrease below about 554 pixels, however, the text box width stops shrinking as quick as necessary to avoid spilling over the left border of the graph.
Adam Danz
Adam Danz 2022년 7월 15일
You must be using regular axes (position == innerposition). With uiaxes, position==outerposition. I'm not suggesting you change to uiaxes, the problem won't change.
About the figure resize, you could also set the "resize" property of the figure to off.
I wonder if you're drawing the annotation textbox too early, before the axes have fully rendered or before you apply the long yticklabels. Does the problem happen if you wait to apply the annotation box? If you provide a minimal working example it would be easier to see what's going on.
FM
FM 2022년 7월 15일
편집: FM 2022년 7월 15일
I think the problem might be the long labels. Here is a minimum working example.
close all
set(gcf,'Position',[100 100 289 274])
subplot(2,2,1)
barh( rand(1,5) );
set(gca,YTickLabel=["One One" "Two Two" "Three Three" "Four Four" "Five Five"]);
annotation('textbox' , ...
get(gca,"InnerPosition") , ...
String='The quick brown fox jump over the lazy dogs.', ...
FitBoxToText='off');
For this example, the annotation box starts to track the axes size properly when the figure width is about 500+ points/pixels.
AFTERNOTE: You may be right about the rendering of the annotation before the labels are rendered. I placed "pause(5)" before the annotation command, and the text box becomes properly sized.
I need to guess at the shortest pause needed and hard code this in. I don't suppose that there is a way to tell Matlab to wait for the completion of one statement before proceeding with the next? Or does the figure simply receive a signal to place labels and, while it is busying doing so, it immediately returns control back to the invoking script?

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

FM
2022년 7월 13일

편집:

FM
2022년 7월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by