Fixed-size text that zooms/scales with a plot?
58 views (last 30 days)
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 on 24 Apr 2013
Edited: Kye Taylor on 24 Apr 2013
Here's a cute little function that does what you're looking for.
% plot some original data
t = linspace(0,2*pi);
x = sin(t);
% get axes size
ax = axis;
% add some text
txt = text(pi/2,1,'Max','fontsize',40/(ax(4)-ax(3)));
h = zoom; % get handle to zoom utility
% 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