Fixed-size text that zooms/scales with a plot?
16 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个评论
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 个)
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.)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!