필터 지우기
필터 지우기

Fixed-size text that zooms/scales with a plot?

조회 수: 40 (최근 30일)
Ken Purchase
Ken Purchase 2013년 4월 24일
When I add text to a plot using the text() command, it appears at a fixed size, and doesn't change as I zoom into the figure. For example, if I draw a rectangle with text inside it, and zoom out until the rectangle is small, the text stays the same size, and thus us now huge compared to the rectangle. I'd like text that stays put inside the rectangle. I guess this is essentially "rendering" text into a plot.
Is there a way in Matlab or something in the user community that can accomplish this? I have searched and haven't found it yet.
Many thanks. Ken

채택된 답변

Kye Taylor
Kye Taylor 2013년 4월 24일
편집: Kye Taylor 2013년 4월 24일
Here's a cute little function that does what you're looking for.
function textWithZoomEffect
% plot some original data
t = linspace(0,2*pi);
x = sin(t);
plot(t,x)
% get axes size
axis([0,2*pi,-1.5,1.5]);
ax = axis;
% add some text
txt = text(pi/2,1,'Max','fontsize',40/(ax(4)-ax(3)));
h = zoom; % get handle to zoom utility
set(h,'ActionPostCallback',@zoomCallBack);
set(h,'Enable','on');
% everytime you zoom in, this function is executed
function zoomCallBack(~, evd)
% Since i expect to zoom in ax(4)-ax(3) gets smaller, so fontsize
% gets bigger.
ax = axis(evd.Axes); % get axis size
% change font size accordingly
set(txt,'FontSize',40/(ax(4)-ax(3)));
end
end
  댓글 수: 2
Ken Purchase
Ken Purchase 2013년 4월 25일
편집: Ken Purchase 2013년 4월 25일
many thanks - the code is quite helpful.
I have an additional question - I'm using a different utility that does the zooming by changing axis limits, rather than going through the zoom utility. This means the zoom "ActionPostCallback" never gets invoked. Is there a similar callback for changing the axis limits? I haven't found it yet in the documentation.
Walter Roberson
Walter Roberson 2013년 4월 25일
I think I saw somewhere that you can add a listener to detect this, but I do not know how one would figure out what the event would be.

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

추가 답변 (2개)

Ken Purchase
Ken Purchase 2013년 5월 2일
Thanks for the help above, Kye and Walter. It helped me get to a nice solution, that rescales with either axis or figure rescaling. I've uploaded it into Matlab Central File Exchange, under the name TextZoomable. It uses some (apparently undocumented?) callbacks attached to both figure rescaling and axis limit changes.

Walter Roberson
Walter Roberson 2013년 4월 24일
zoom PostAction callback, detect the current axis xlim and ylim, decide on a scaling, and set() the text FontSize appropriately (might as well only do it for the objects that are visible.)
  댓글 수: 1
Ken Purchase
Ken Purchase 2013년 4월 25일
Great - thanks! (not sure I know how to detect which objects are visible - do you mean they're just within xlim, ylim?)

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

카테고리

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